Finding a public facing IP address in Python?

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


Asked by: Joyce136 | Posted: 01-10-2021






Answer 1

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.

Answered by: Adelaide801 | Posted: 02-11-2021



Answer 2

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

Answered by: Miller885 | Posted: 02-11-2021



Answer 3

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.

Answered by: David612 | Posted: 02-11-2021



Answer 4

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

Answered by: Joyce658 | Posted: 02-11-2021



Answer 5

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

Answered by: Ryan810 | Posted: 02-11-2021



Answer 6

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.

Answered by: Owen956 | Posted: 02-11-2021



Answer 7

This is simple as

>>> import urllib
>>> urllib.urlopen('http://icanhazip.com/').read().strip('\n')
'xx.xx.xx.xx'

Answered by: Miller981 | Posted: 02-11-2021



Answer 8

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

Answered by: Daryl802 | Posted: 02-11-2021



Similar questions

python - Scapy ARP not found error, finding Mac Address of Amazon Dash button

So I'm attempting to follow this guide to find the mac address of an Amazon dash button. I'm on a Windows machine running Python 2.6 and scapy. I'm fairly certain that I followed these directions correctly on...


Finding source IP address in Python

I am trying to find source IP address of the received packet using python 3, but without success. Does anyone has an idea how to do that? I want to reply to multicast message with unicast message to the sender and also make a list of senders IP addresses. Thank you


Finding first and last index of some value in a list in Python

Is there any built-in methods that are part of lists that would give me the first and last index of some value, like: verts.IndexOf(12.345) verts.LastIndexOf(12.345)


python - Regex for finding date in Apache access log

I'm writing a python script to extract data out of our 2GB Apache access log. Here's one line from the log. 81.52.143.15 - - [01/Apr/2008:00:07:20 -0600] "GET /robots.txt HTTP/1.1" 200 29 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1) VoilaBot BETA 1.2 (http://www.voila.com/)" I'm trying to get the date portion from that line, and regex is failing me, and I'm not sure why. H...


regex - Finding partial strings in a list of strings - python

I am trying to check if a user is a member of an Active Directory group, and I have this: ldap.set_option(ldap.OPT_REFERRALS, 0) try: con = ldap.initialize(LDAP_URL) con.simple_bind_s(userid+"@"+ad_settings.AD_DNS_NAME, password) ADUser = con.search_ext_s(ad_settings.AD_SEARCH_DN, ldap.SCOPE_SUBTREE, \ "sAMAccountName=%s" % userid, ad_settings.AD_SEARCH_FIELDS)[0][1] except ldap.LDAPError...


python - finding firefox version

How to find Firefox version using python?


python - Finding and adding twitter users?

Any suggestions for a good twitter library (preferably in Ruby or Python)? I have a list of usernames, and I need to be able to programmatically follow these users. I tried twitter4r in Ruby, but finding users doesn't seem to work. When I do twitter = Twitter::Client.new(:login =&gt; 'mylogin', :password =&gt; 'mypassword') user = Twitter::User.find('ev', twitter) ...


python - Finding perfect square

I have this python code: def sqrt(x): ans = 0 if x &gt;= 0: while ans*ans &lt; x: ans = ans + 1 if ans*ans != x: print x, 'is not a perfect square.' return None else: print x, ' is a perfect square.' return ans else: print x, ' is not a positive number.' return None y = 16...


python - Finding the highest key

I'm just confused about why my code would not work, here's the question and the code I have so far (the test run says my answer is wrong). Given the dictionary d, find the largest key in the dictionary and associate the corresponding value with the variable val_of_max. For example, given the dictionary {5:3, 4:1, 12:2}, 2 would be associated with val_of_max. Assu...


python - Django test runner not finding tests

I am new to both Python and Django and I'm learning by creating a diet management site but I've been completely defeated by getting my unit tests to run. All the docs and blogs I've found say that as long as it's discoverable from tests.py, tests.py is in the same folder as models.py and your test class subclasses TestCase, it should all get picked up automatically. This isn't working for me, when I run manage.py t...


Finding fast default aliases in Python

Is there a faster way to do the following for much larger dicts? aliases = { 'United States': 'USA', 'United Kingdom': 'UK', 'Russia': 'RUS', } if countryname in aliases: countryname = aliases[countryname]


Finding Top Twitter Friends Using Python

What's the best way to find someone's top friends on twitter? I'm trying to figure out a way to see who they interact with the most, but I am not sure what the best way is of doing this. Also, there are obvious things to check (is the username following that person, etc etc). I imagine someone else has thought about this, so I am trying to get a little smarter on it. I am using Python to figure this out. ...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top