-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbitly_http.py
80 lines (66 loc) · 2.1 KB
/
bitly_http.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
74
75
76
77
78
79
80
"""
This class is an abstracted http handler that uses multiple underlying http
libraries
it will default to
a) pycurl
b) urllib2
"""
try:
import pycurl
PYCURL = True
except ImportError:
PYCURL = False
import urllib.request
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
class DontRedirect(urllib.request.HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl):
if code in (301, 302, 303, 307):
raise urllib.request.HTTPError(req.get_full_url(), code, msg, headers, fp)
def makeUrllib2Http(url, user_agent):
dont_redirect = DontRedirect()
opener = urllib.request.build_opener(dont_redirect)
opener.addheaders = [('User-agent', user_agent + ' urllib')]
try:
response = opener.open(url)
code = response.code
data = response.read()
except urllib.request.URLError as e:
return 500, str(e)
except urllib.request.HTTPError as e:
code = e.code
data = e.read()
return code, data
def makePycurlHttp(url, timeout, user_agent):
try:
buffer = StringIO.StringIO()
curl = pycurl.Curl()
curl.setopt(pycurl.TIMEOUT_MS, timeout)
curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.FOLLOWLOCATION, 0)
curl.setopt(pycurl.MAXREDIRS, 0)
curl.setopt(pycurl.WRITEFUNCTION, buffer.write)
curl.setopt(pycurl.NOSIGNAL, 1)
curl.setopt(pycurl.USERAGENT, user_agent + ' ' + pycurl.version)
# if referer:
# curl.setopt(pycurl.REFERER, referer)
curl.perform()
result = buffer.getvalue()
buffer.close()
http_status_code = curl.getinfo(pycurl.HTTP_CODE)
curl.close()
except Exception:
try:
curl.close()
except Exception:
pass
raise
return http_status_code, result
def get(url, timeout, user_agent):
if PYCURL:
code, result = makePycurlHttp(url, timeout, user_agent)
else:
code, result = makeUrllib2Http(url, user_agent)
return {'http_status_code': code, 'result': result}