Finding a public facing IP address in Python? Finding a public facing IP address in Python? python python

Finding a public facing IP address in Python?


This will fetch your remote IP address

import urllibip = urllib.urlopen('http://automation.whatismyip.com/n09230945.asp').read()

If you don't want to rely on someone else, then just upload something like this PHP script:

<?php echo $_SERVER['REMOTE_ADDR']; ?>

and change the URL in the Python or if you prefer ASP:

<%Dim UserIPAddressUserIPAddress = Request.ServerVariables("REMOTE_ADDR")%>

Note: I don't know ASP, but I figured it might be useful to have here so I googled.


https://api.ipify.org/?format=json is pretty straight forward

can be parsed by just running requests.get("https://api.ipify.org/?format=json").json()['ip']


whatismyip.org is better... it just tosses back the ip as plaintext with no extraneous crap.

import urllibip = urllib.urlopen('http://whatismyip.org').read()

But yeah, it's impossible to do it easily without relying on something outside the network itself.