2

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!

0

2 Answers 2

2

You have two separate problems.

  1. You need to convert answer to a string, even though answer has some funny characters that do not decode well with utf-8.

  2. You are invoking the regular expressions API incorrectly.

Here is a corrected version, which uses chr to work around issue 1, and fixes issue 2 with the correct syntax.

#!/usr/bin/python
from sys import exit
import urllib.request
import re


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.
    '''
    answer = "".join([chr(x) for x in answer])
    pattern = "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
    regexp = re.compile(pattern)
    m = regexp.search(answer)
    if m:
        ip = m.group(0)
        return ip
    else:
        raise RuntimeError

answer = urllib.request.urlopen("http://monip.org").read()

try:
    ip = parse_answer(answer)
except RuntimeError:
    print("Error, check your network configuration.")
    print("Aborting..")
    exit(1)

print("IP:", ip)
Sign up to request clarification or add additional context in comments.

Comments

1

If you'll try to:

print answer

you'll fail because it's encoded in ISO-8859-1.

You should convert it first to UTF-8 before sending it to parse_answer():

answer = answer.encode('utf8')

Once you'll pass that hurdle you'll run into another error which relies in the following two lines:

if regexp.match(regexp, answer):
    m = regexp.search(regexp, answer) 

since regex is already a compiled pattern, you shouldn't send it as an argument in any of the two calls above! change the code to:

if regexp.match(answer):
    m = regexp.search(answer) 

and it should work!


For Merlin:

import requests
answer = requests.get("http://monip.org")
print answer.text.encode('utf8')

OUTPUT

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>MonIP.org v1.0</title>
<META http-equiv="Content-type" content="text/html; charset=ISO-8859-1">
</head>
<P ALIGN="center"><FONT size=8><BR>IP : 50.184.3.115<br></font><font size=3><i>c-50-184-3-115.hsd1.ca.comcast.net</i><br></font><font size=1><br><br>Pas de proxy détecté - No Proxy detected</font></html>

2 Comments

@merlin2011 I did test this solution and I'll post the code in a minute (especially for you ;)
Thank you. I appreciate it. Your method of calling the requests library is completely different from the OP's method, so your answer is now much more useful. +1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.