So, here's the code :
#!/usr/bin/python
from sys import exit
import urllib.request
answer = urllib.request.urlopen("http://monip.org").read()
def debug(txt):
print(txt)
exit(0)
def parse_answer(answer):
''' Simple function to parse request's HTML result
to find the ip in it. Raise RuntimeError if no
ip in result and ip else.
'''
import re
pattern = "^\w+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\w+$"
regexp = re.compile(pattern)
if regexp.match(regexp, answer):
m = regexp.search(regexp, answer)
ip = m.group(0)
return ip
else:
raise RuntimeError
try:
ip = parse_answer(answer)
except RuntimeError:
print("Error, check your network configuration.")
print("Aborting..")
exit(1)
print("IP:", ip)
I wrote that. This code is intended to give you your public ip adress. It throws a RunTime error if it cannot give you anything.
And here's the error :
Traceback (most recent call last): File "./ippub", line 27, in ip = parse_answer(answer) File "./ippub", line 19, in parse_answer if regexp.match(regexp, answer): TypeError: 'bytes' object cannot be interpreted as an integer
That means "answer" variable is bytes, but I wanna match an ip adress in it, and I can't because of python type system :-)
Any idea ? Many thanks!