Search code examples
pythoncurlpycurl

Loop to check if a variable has changed in Python


I have just learned the basics of Python, and I am trying to make a few projects so that I can increase my knowledge of the programming language.

Since I am rather paranoid, I created a script that uses PycURL to fetch my current IP address every x seconds, for VPN security. Here is my code[EDITED]:

import requests

enterIP = str(input("What is your current IP address?"))

def getIP():
    while True:
        try:
             result = requests.get("http://ipinfo.io/ip")
             print(result.text)
        except KeyboardInterrupt:
             print("\nProccess terminated by user")
    return result.text

def checkIP():
    while True:
        if enterIP == result.text:
            pass
         else:
            print("IP has changed!")

getIP()
checkIP()

Now I would like to expand the idea, so that the script asks the user to enter their current IP, saves that octet as a string, then uses a loop to keep running it against the PycURL function to make sure that their IP hasn't changed? The only problem is that I am completely stumped, I cannot come up with a function that would take the output of PycURL and compare it to a string. How could I achieve that?


Solution

  • As @holdenweb explained, you do not need pycurl for such a simple task, but nevertheless, here is a working example:

    import pycurl
    import time
    from StringIO import StringIO
    
    def get_ip():
        buffer = StringIO()
        c = pycurl.Curl()
        c.setopt(pycurl.URL, "http://ipinfo.io/ip")
        c.setopt(c.WRITEDATA, buffer)
        c.perform()
        c.close()
        return buffer.getvalue()
    
    def main():
        initial = get_ip()
        print 'Initial IP: %s' % initial
    
        try:
            while True:
                current = get_ip()
                if current != initial:
                    print 'IP has changed to: %s' % current
                time.sleep(300)
        except KeyboardInterrupt:
            print("\nProccess terminated by user")
    
    if __name__ == '__main__':
        main()
    

    As you can see I moved the logic of getting the IP to separate function: get_ip and added few missing things, like catching the buffer to a string and returning it. Otherwise it is pretty much the same as the first example in pycurl quickstart

    The main function is called below, when the script is accessed directly (not by import).

    First off it calls the get_ip to get initial IP and then runs the while loop which checks if the IP has changed and lets you know if so.

    EDIT: Since you changed your question, here is your new code in a working example:

    import requests
    
    def getIP():
        result = requests.get("http://ipinfo.io/ip")
        return result.text
    
    def checkIP():
        initial = getIP()
        print("Initial IP: {}".format(initial))
        while True:
            current = getIP()
            if initial == current:
                pass                                
            else:
                print("IP has changed!")
    
    checkIP()
    

    As I mentioned in the comments above, you do not need two loops. One is enough. You don't even need two functions, but better do. One for getting the data and one for the loop. In the later, first get initial value and then run the loop, inside which you check if value has changed or not.