This repository has been archived by the owner on Mar 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
get_ip.py
69 lines (61 loc) · 2.33 KB
/
get_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
import re
import logging
import socket
from urllib import request, error, parse
# 匹配合法 IP 地址
regex_ip = re.compile(
r"\D*("
+ r"(?:1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\."
+ r"(?:1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\."
+ r"(?:1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\."
+ r"(?:1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)"
+ r")\D*")
# 增强鲁棒性,用多种方式获取 IP
def get_ip():
return (get_ip_by_ipip()
or get_ip_by_httpbin()
or get_ip_by_httpbin_direct_1()
or get_ip_by_httpbin_direct_2() )
# 这几个函数会在 DNS 遭受污染时失效
def get_ip_by_ipip():
url = 'http://myip.ipip.net/'
try:
resp = request.urlopen(url=url, timeout=10).read()
return regex_ip.match(resp.decode("utf-8")).group(1)
except Exception as e:
logging.warning("get_ip_by_ipip FAILED, error: %s", str(e))
return None
def get_ip_by_httpbin():
url = 'http://www.httpbin.org/ip'
try:
resp = request.urlopen(url=url, timeout=10).read()
return regex_ip.match(resp.decode("utf-8")).group(1)
except Exception as e:
logging.warning("get_ip_by_httpbin FAILED, error: %s", str(e))
return None
# 这个函数可以在本地 DNS 遭受污染的时候获取到IP
# 如需模拟DNS污染,可以在HOSTS文件里加入 127.0.0.1 www.httpbin.org
def get_ip_by_httpbin_direct_1():
url = 'http://52.5.182.176/ip'
try:
req = request.Request(url=url, method='GET', headers={'Host': 'www.httpbin.org'})
resp = request.urlopen(req).read()
return regex_ip.match(resp.decode("utf-8")).group(1)
except Exception as e:
logging.warning("get_ip_by_httpbin_direct_1 FAILED, error: %s", str(e))
return None
def get_ip_by_httpbin_direct_2():
url = 'http://52.44.230.61/ip'
try:
req = request.Request(url=url, method='GET', headers={'Host': 'www.httpbin.org'})
resp = request.urlopen(req).read()
return regex_ip.match(resp.decode("utf-8")).group(1)
except Exception as e:
logging.warning("get_ip_by_httpbin_direct_2 FAILED, error: %s", str(e))
return None
# 测试
if __name__ == '__main__':
print(get_ip() )
print(get_ip_by_ipip() )
print(get_ip_by_httpbin() )
print(get_ip_by_httpbin_direct_1() )