-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebHandler.py
94 lines (76 loc) · 2.67 KB
/
webHandler.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
# encoding: utf-8
import urllib
import urllib2
import cookielib
class WebException(Exception):
'''
handle exception
'''
pass
class WebHandler(object):
'''
a handler to deal with webrequest: coookie,get or post,etc.
'''
def __init__(self):
self.opener = urllib2.build_opener()
self.EnableCookie()
def Request(self, url, data=None, headers={}, method='GET', type=''):
'''
a wrapper to send a request
'''
# check url invalid or not
if url is None or url == '':
raise WebException('url is necessary')
# add User-Agent to headers
if 'User-Agent' not in headers:
headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36 LBBROWSER'
self.opener.addheaders = headers.items()
try:
if data is None:
req = urllib2.Request(url)
elif not isinstance(data, dict):
raise WebException('data is invalid!')
else:
if method == 'GET':
req = urllib2.Request(url + '?' + urllib.urlencode(data))
elif method == 'POST':
req = urllib2.Request(url, data=urllib.urlencode(data))
page = urllib2.urlopen(req, timeout=20).read()
if type != '':
return page
# decode to Unicode
try:
page = page.decode('utf-8')
except:
page = page.decode('gbk', 'ignore')
return page
except urllib2.HTTPError, e:
return WebException(e.fp.read(), '', e.headers, e.code)
def EnableCookie(self):
'''
enable cookie handle, using after your login
'''
self.cookiejar = cookielib.CookieJar()
self.cookieproc = urllib2.HTTPCookieProcessor(self.cookiejar)
self.opener.add_handler(self.cookieproc)
# self.opener = urllib2.build_opener(self.cookieproc)
urllib2.install_opener(self.opener)
def EnableProxy(self, proxyDict):
'''
Enable use proxy to request:
proxyDict: {"http":"115.182.83.38:8080"}
'''
self.opener.add_handler(urllib2.ProxyHandler(proxyDict))
def getCookie(self, key=None):
'''
Get cookie from CookieJar by key if key is valid
'''
if key is None:
return {x.name: x.value for x in self.cookiejar}
else:
for cookie in self.cookiejar:
if cookie.name == key:
return cookie.value
else:
return ''