-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip.py
73 lines (51 loc) · 1.38 KB
/
ip.py
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
#!/usr/bin/env python
"""
Description
ip.py - get public IP address
Usage
python ip.py
Output
https://duckduckgo.com/?q=what+is+my+ip&ia=answer:
IP: x.x.x.x
http://checkip.dyndns.com/:
IP: x.x.x.x
Note
- works with python 2.7 and 3.6
Author
David Laperriere <[email protected]>
"""
import re
import sys
import time
try:
import urllib.request as urllib2
except ImportError:
import urllib2
__version_info__ = (1, 0)
__version__ = '.'.join(map(str, __version_info__))
__author__ = "David Laperriere [email protected]"
def main():
timeout = 3
urls = ["https://duckduckgo.com/?q=what+is+my+ip&ia=answer",
"https://ipinfo.io/",
"http://checkip.dyndns.com/"]
ip_regexp = re.compile(b'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
for url in urls:
try:
request = urllib2.Request(url)
response = urllib2.urlopen(request)
except urllib2.URLError as e:
print(url + " error : \n" + str(e.reason) + "\n")
continue
print(url + ":\n")
data = response.read()
ip = ip_regexp.search(data)
if ip:
print("IP: {}".format(ip.group().decode("utf-8")))
sys.exit(0)
else:
print("not found")
print(" ")
time.sleep(timeout)
if __name__ == "__main__":
main()