forked from madmouser1/ipgetter
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathipgetter.py
More file actions
executable file
·195 lines (164 loc) · 6.58 KB
/
ipgetter.py
File metadata and controls
executable file
·195 lines (164 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python
"""
This module 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 serverlist to minimize request
overhead on a single server
If you want to add or remove your server from the list contact me on github
API Usage
=========
>>> import ipgetter
>>> myip = ipgetter.myip()
>>> myip
'8.8.8.8'
>>> ipgetter.IPgetter().test()
Number of servers: 47
IP's :
8.8.8.8 = 47 ocurrencies
"""
import re
import random
import signal
from sys import version_info
from functools import wraps
PY3K = version_info >= (3, 0)
if PY3K:
import urllib.request as urllib
else:
import urllib2 as urllib
__version__ = "0.4"
def timeout(seconds, error_message='Function call timed out'):
'''
Decorator that provides timeout to a function
'''
def decorated(func):
def _handle_timeout(signum, frame):
raise TimeoutError(error_message)
@wraps(func)
def wrapper(*args, **kwargs):
signal.signal(signal.SIGALRM, _handle_timeout)
signal.alarm(seconds)
try:
result = func(*args, **kwargs)
finally:
signal.alarm(0)
return result
return wrapper
return decorated
@timeout(120)
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 serverlist to minimize request overhead
on a single server
'''
def __init__(self):
self.server_list = ['http://ip.dnsexit.com',
'http://ifconfig.me/ip',
'http://ipecho.net/plain',
'http://checkip.dyndns.org/plain',
'http://ipogre.com/linux.php',
'http://whatismyipaddress.com/',
'http://ip.my-proxy.com/',
'http://websiteipaddress.com/WhatIsMyIp',
'http://getmyipaddress.org/',
'http://showmyipaddress.com/',
'http://www.my-ip-address.net/',
'http://myexternalip.com/raw',
'http://www.canyouseeme.org/',
'http://www.trackip.net/',
'http://myip.dnsdynamic.org/',
'http://icanhazip.com/',
'http://www.iplocation.net/',
'http://www.howtofindmyipaddress.com/',
'http://www.ipchicken.com/',
'http://whatsmyip.net/',
'http://www.ip-adress.com/',
'http://checkmyip.com/',
'http://www.tracemyip.org/',
'http://checkmyip.net/',
'http://www.lawrencegoetz.com/programs/ipinfo/',
'http://www.findmyip.co/',
'http://ip-lookup.net/',
'http://www.dslreports.com/whois',
'http://www.mon-ip.com/en/my-ip/',
'http://www.myip.ru',
'http://ipgoat.com/',
'http://www.myipnumber.com/my-ip-address.asp',
'http://www.whatsmyipaddress.net/',
'http://formyip.com/',
'https://check.torproject.org/',
'http://www.displaymyip.com/',
'http://www.bobborst.com/tools/whatsmyip/',
'http://www.geoiptool.com/',
'https://www.whatsmydns.net/whats-my-ip-address.html',
'https://www.privateinternetaccess.com/pages/whats-my-ip/',
'http://checkip.dyndns.com/',
'http://myexternalip.com/',
'http://www.ip-adress.eu/',
'http://www.infosniper.net/',
'http://wtfismyip.com/',
'http://ipinfo.io/',
'http://httpbin.org/ip']
def get_externalip(self):
'''
This function gets your IP from a random server
'''
random.shuffle(self.server_list)
myip = ''
for server in self.server_list:
myip = self.fetch(server)
if myip != '':
return myip
else:
continue
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")]
try:
url = opener.open(server)
content = url.read()
# Didn't want to import chardet. Prefered to stick to stdlib
if PY3K:
try:
content = content.decode('UTF-8')
except UnicodeDecodeError:
content = content.decode('ISO-8859-1')
m = re.search(
'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)',
content)
myip = m.group(0)
return myip if len(myip) > 0 else ''
except Exception:
return ''
finally:
if url:
url.close()
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'))
print('\n')
print(resultdict)
if __name__ == '__main__':
print(myip())