forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_proxy.py
More file actions
104 lines (90 loc) · 2.47 KB
/
test_proxy.py
File metadata and controls
104 lines (90 loc) · 2.47 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
# encoding: utf-8
__author__ = 'zhanghe'
import requests
import re
import json
def test(ip=None):
"""
正向代理测试
"""
if ip is None:
ip_str = ''
get_ip_info_fuc = 'get_ip_info'
else:
ip_str = str(ip)
# todo ip地址合法性校验
get_ip_info_fuc = 'get_ip_info_by_ip'
source_site = 'http://ip.cn/index.php'
header = {
'Host': 'ip.cn',
'Referer': 'http://ip.cn/',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36'
}
s = requests.session()
# 查询参数
payload = {
# 'ip': '101.81.89.102'
# 'ip': '223.167.32.101'
'ip': ip_str
}
# 代理配置
proxies = {
'http': 'http://192.168.111.129:8888' # 代理服务
# 'http': 'http://202.38.128.221:8086' # 北京市 高能物理所
}
# 请求页面
try:
# r = s.get(source_site, params=payload, headers=header)
r = s.get(source_site, params=payload, headers=header, proxies=proxies)
print eval(get_ip_info_fuc)(r.content)
except:
print '代理连接失败'
def get_ip_info(html):
"""
查询当前的IP信息
:param html:
:return:
"""
reg_rule = r'<div id="result"><div class="well"><p>当前 IP:<code>(.+?)</code> 来自:(.+?)</p><p>GeoIP: (.+?)</p></div></div>'
reg = re.compile(reg_rule)
try:
html_list = re.findall(reg, html)
return json.dumps(html_list[0], ensure_ascii=False, indent=4)
except:
return None
def get_ip_info_by_ip(html):
"""
查询具体IP地址的详细信息
:param html:
:return:
"""
reg_rule = r'<div id="result"><div class="well"><p>查询的 IP:<code>(.+?)</code> 来自:(.+?)</p><p>(.+?)</p><p>(.+?)</p></div></div>'
reg = re.compile(reg_rule)
try:
html_list = re.findall(reg, html)
return json.dumps(html_list[0], ensure_ascii=False, indent=4)
except:
return None
if __name__ == "__main__":
test('42.51.13.103')
test()
"""
测试:
zhanghe@ubuntu:~/code/python$ python test/normal_proxy/test_proxy.py
[
"202.38.128.221",
"北京市 高能物理所",
"Beijing, China"
]
[
"42.51.13.103",
"河南省郑州市 电联通信技术",
"GeoIP: Zhengzhou, Henan, China",
"Henan Telcom Union Technology Co., LTD"
]
[
"223.167.32.101",
"上海市 联通",
"Shanghai, China"
]
"""