5

Trying to get my head arround why I cannot match the output of the IP against a set IP and therefore render a outcome.

import urllib
import re

ip = '212.125.222.196'

url = "http://checkip.dyndns.org"

print url

request = urllib.urlopen(url).read()

theIP = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}", request)

print "your IP Address is: ",  theIP

if theIP == '211.125.122.192':
    print "You are OK"
else:
    print "BAAD"

The result is always "BAAD"

6
  • Just a suggestion - you can use icanhazip.com for a cleaner output of the requesting IP. Commented Nov 6, 2013 at 15:38
  • 1
    @Lix Same thing at curlmyip.com Commented Nov 6, 2013 at 15:58
  • @TankorSmash - good to know there is an alternative! Still like mine better though :P Commented Nov 6, 2013 at 15:59
  • Did you ever get this working? Commented Nov 7, 2013 at 8:12
  • Yes, working thank to your help!:) Next challenge up.. But will aim to solve it prior to asking again.. This site is amazing and great people here! Glad that you are willing to help us newbies asking dumb questions. :) Will check out your suggestions on icanhazip and curlmyip as well since I´m getting errors from dyndns every second time running the script.. Commented Nov 8, 2013 at 20:53

4 Answers 4

6

re.findall returns a list of matches, not a string. So you've two options now, either iterate over the list and use any:

theIP = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}", request)
if any(ip == '211.125.122.192' for ip in theIP):
    print "You are OK"
else:
    print "BAAD"  

#or simply:

if '211.125.122.192' in theIp:
    print "You are OK"
else:
    print "BAAD"  

or use re.search:

theIP = re.search(r"\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}", request)
if theIP and (theIP.group() == '211.125.122.192'):
    print "You are OK"
else:
    print "BAAD"  
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for a great and easy explanation! :) It really shows how much of a beginner I am..
0

It is because you compare a list with a string. Possible solutions (depending on what you want):

if any(ip == '211.125.122.192' for ip in theIP):

-> checks whether any of found IP addresses match

or

if theIP and theIP[0] == '211.125.122.192':

-> checks if list not empty and if first found IP address matches.

If the result will always contain only one IP address, then instead of re.findall you can use just re.search, as proposed by hcwhsa.

Comments

0

re.findAll returns a list, not a string!

You'll have to grab the string:

theIP = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}", request)[0]

Or, just check if ip is included in the search result:

if ip in theIP:
    print "You are OK"

Or use re.search:

theIP = re.search(r"\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}", request)

Comments

0

theIP is not a string, it is a list. See the documentation

>>> print re.findall.__doc__
Return a list of all non-overlapping matches in the string.

    If one or more groups are present in the pattern, return a
    list of groups; this will be a list of tuples if the pattern
    has more than one group.

    Empty matches are included in the result.

You might want to do something like

for ip in theIP:
    if ip == '211.125.122.192':
        print 'You are ok :)'

However, there is probably a much better way of getting your ip than hitting a web page and parsing the result. Maybe you could use hostname -I and subprocess? Maybe something like this would work better?

import subprocess

theIP = subprocess.check_output(['hostname','-I'])

1 Comment

Not sure wether hostname would help me in this case since it´s the external IP I am after, not the one for the server as such. Or would it? :) Will try to use alternatives mentioned in the main comments though.

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.