External IP Address Search Using Python Source Code

October 18, 2017 | Views: 6764

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

This time, I’m providing a Python source code tool designed to fetch your external IP address from the Internet. This is used mostly when you’re behind a NAT. It picks your IP randomly from a server list to minimize request overhead on a single server.

You need to import re and random libraries for this tool to perfectly run.


Here is the source code:
`#!/usr/bin/env python

API Usage

>>> import ipgetter
>>> myip = ipgetter.myip()
>>> myip

>>> ipgetter.IPgetter().test()

Number of servers: 47
IP’s : = 47 ocurrencies

import re
import random

PY3K = version_info >= (3, 0)

if PY3K:
import urllib.request as urllib
import urllib2 as urllib

__version__ = “0.4”

def myip():
return IPgetter().get_externalip()

class IPgetter(object):

This class is designed to fetch your external IP address from the internet.
It is used mostly when behind a NAT.
It picks your IP randomly from a server list to minimize request overhead
on a single server.

def __init__(self):
self.server_list = [‘http://ip.dnsexit.com’,

def get_externalip(self):
This function gets your IP from a random server

myip = ”
for i in range(7):
myip = self.fetch(random.choice(self.server_list))
if myip != ”:
return myip
return ”

def fetch(self, server):
This function gets your IP from a specific server.
url = None
opener = urllib.build_opener()
opener.addheaders = [(‘User-agent’,
“Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0″)]

url = opener.open(server, timeout=2)
content = url.read()

# Didn’t want to import chardet. Prefered to stick to stdlib
if PY3K:
content = content.decode(‘UTF-8’)
except UnicodeDecodeError:
content = content.decode(‘ISO-8859-1’)

m = re.search(
myip = m.group(0)
return myip if len(myip) > 0 else ”
except Exception:
return ”
if url:

def test(self):
This functions tests the consistency of the servers
on the list when retrieving your IP.
All results should be the same.

resultdict = {}
for server in self.server_list:
resultdict.update(**{server: self.fetch(server)})

ips = sorted(resultdict.values())
ips_set = set(ips)
print(‘nNumber of servers: {}’.format(len(self.server_list)))
print(“IP’s :”)
for ip, ocorrencia in zip(ips_set, map(lambda x: ips.count(x), ips_set)):
print(‘{0} = {1} ocurrenc{2}’.format(ip if len(ip) > 0 else ‘broken server’, ocorrencia, ‘y’ if ocorrencia == 1 else ‘ies’))

if __name__ == ‘__main__’:


I hope this source code tool can be useful to you. If you have any questions you can contact me on the comments section or send me a message.

Share with Friends
Use Cybytes and
Tip the Author!
Share with Friends
Ready to share your knowledge and expertise?
Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Cybrary On The Go

Get the Cybrary app for Android for online and offline viewing of our lessons.

Get it on Google Play

Support Cybrary

Donate Here to Get This Month's Donor Badge

Skip to toolbar

We recommend always using caution when following any link

Are you sure you want to continue?