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
110 lines (86 loc) · 2.65 KB
/
test_proxy.py
File metadata and controls
110 lines (86 loc) · 2.65 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
# encoding: utf-8
__author__ = 'zhanghe'
import requests
def test_lan():
"""
局域网反向代理测试
"""
source_site = 'http://phalcon/'
header = {
# 'Host': 'phalcon',
'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()
proxies = {
'http': 'http://192.168.59.139:80'
}
# 请求页面
r = s.get(source_site, headers=header, proxies=proxies)
# print r.headers
# print r.encoding
print r.content
def test_wan():
"""
广域网正向代理测试
"""
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'
}
# 代理配置
proxies = {
'http': 'http://127.0.0.1:8888' # 代理服务
}
# 请求页面
try:
# r = s.get(source_site, params=payload, headers=header)
r = s.get(source_site, params=payload, headers=header, proxies=proxies)
print get_ip_info(r.content)
except:
print '代理连接失败'
def get_ip_info(html):
"""
通过正则表达式获取页面IP信息
:param html:
:return:
"""
import re
import json
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_lan()
test_wan()
"""
局域网测试:
日志监控
$ tail -f /var/log/nginx/access.log
不添加header:
127.0.0.1 - - [30/Aug/2015:14:08:24 +0800] "GET / HTTP/1.1" 200 343 "-" "python-requests/2.5.0 CPython/2.7.6 Linux/3.13.0-36-generic" "-"
附加上header:
127.0.0.1 - - [30/Aug/2015:14:30:53 +0800] "GET / HTTP/1.1" 200 343 "-" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36" "-"
设置代理:
192.168.59.139 - - [30/Aug/2015:22:22:17 +0800] "GET http://phalcon/ HTTP/1.1" 200 343 "-" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36" "-"
"""
"""
广域网测试:
[
"101.81.89.102",
"上海市 电信",
"GeoIP: Shanghai, China",
"China Telecom Shanghai"
]
"""