-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpclient.py
48 lines (40 loc) · 1.12 KB
/
httpclient.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import chardet
import sys
import traceback
__author__ = 'Tony.Shao'
#通过代理抓取用户Timeline
from geventhttpclient import HTTPClient
from geventhttpclient import URL
proxies = {
'http': '127.0.0.1:8087'
}
PROXY_IP = '127.0.0.1'
headers = {
'X-Forwarded-For': '%s, 127.0.0.1, 192.168.0.1' % PROXY_IP,
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.43 Safari/536.11'
}
_ver = sys.version_info
#: Python 2.x?
is_py2 = (_ver[0] == 2)
#: Python 3.x?
is_py3 = (_ver[0] == 3)
def _http_call(url):
try:
url = URL(url=url)
client = HTTPClient.from_url(url, concurrency=20)
uri = url.request_uri
resp = client.get(uri, headers=headers)
return resp
except Exception:
print traceback.format_exc()
return None
http_call = _http_call
def _read_body(resp, encoding=None):
content = resp.read()
if encoding is None:
encoding = chardet.detect(content)['encoding']
if is_py2:
return unicode(content, encoding=encoding)
read_body = _read_body