21

How can I find the public facing IP for my net work in Python?

Bobby Jack
  • 15,689
  • 15
  • 65
  • 97
UnkwnTech
  • 88,102
  • 65
  • 184
  • 229
  • Yes I asked this with the intention of answering it, but it was not here and I could see someone else needing the info. – UnkwnTech Oct 03 '08 at 12:22
  • All of the answers I see above would report the IP address of any web proxy in use, not necessarily the public facing IP address of your system (anything not being run through a web proxy may have an entirely different IP address). – Keith Schoenefeld Jun 01 '12 at 00:35

8 Answers8

14

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']

twig
  • 4,034
  • 5
  • 37
  • 47
13

This will fetch your remote IP address

import urllib
ip = 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 UserIPAddress
UserIPAddress = Request.ServerVariables("REMOTE_ADDR")
%>

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

UnkwnTech
  • 88,102
  • 65
  • 184
  • 229
  • 1
    [php manual](http://www.php.net/manual/en/reserved.variables.server.php) says that `REMOTE_ADDR` is *"The IP address from which the user is viewing the current page."* i.e., you need to put the php script on an external server to get your public ip. Also `automation.whatismyip.com` is not accessible. `http://canihazip.com/s` works. – jfs Feb 24 '14 at 21:41
  • 1
    It seems automation.whatismyip.com is no longer running, something about they want you to make an account and use their home page. – Ian M Feb 08 '15 at 05:45
  • whatsmyip.com/org seem to not allow requests from unknown user-agents anymore. api.ipify.org does not seem to have that issue. Let me see if I can edit this answer. – Rubin Simons Oct 02 '20 at 09:44
6

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

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

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

Steve Losh
  • 19,642
  • 2
  • 51
  • 44
6
import requests
r = requests.get(r'http://jsonip.com')
# r = requests.get(r'https://ifconfig.co/json')
ip= r.json()['ip']
print('Your IP is {}'.format(ip))

Reference

Abhijeet
  • 8,561
  • 5
  • 70
  • 76
4

If you don't mind expletives then try:

http://wtfismyip.com/json

Bind it up in the usual urllib stuff as others have shown.

There's also:

http://www.networksecuritytoolkit.org/nst/tools/ip.php

powlo
  • 2,538
  • 3
  • 28
  • 38
3
import urllib2
text = urllib2.urlopen('http://www.whatismyip.org').read()
urlRE=re.findall('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',text)
urlRE        

['146.148.123.123']

Try putting whatever 'findmyipsite' you can find into a list and iterating through them for comparison. This one seems to work well.

1

This is simple as

>>> import urllib
>>> urllib.urlopen('http://icanhazip.com/').read().strip('\n')
'xx.xx.xx.xx'
loretoparisi
  • 15,724
  • 11
  • 102
  • 146
0

You can also use DNS which in some cases may be more reliable than http methods:

#!/usr/bin/env python3

# pip install --user dnspython

import dns.resolver

resolver1_opendns_ip = False
resolver = dns.resolver.Resolver()
opendns_result = resolver.resolve("resolver1.opendns.com", "A")
for record in opendns_result:
    resolver1_opendns_ip = record.to_text()

if resolver1_opendns_ip:
    resolver.nameservers = [resolver1_opendns_ip]
    myip_result = resolver.resolve("myip.opendns.com", "A")
    for record in myip_result:
        print(f"Your external ip is {record.to_text()}")

This is the python equivalent of dig +short -4 myip.opendns.com @resolver1.opendns.com

htaccess
  • 2,800
  • 26
  • 31