Skip to content

Commit a25eb07

Browse files
committed
maintain HttpWrapper
1 parent 67208a7 commit a25eb07

File tree

5 files changed

+264
-1
lines changed

5 files changed

+264
-1
lines changed

ProdikaUtility.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Prodika(object):
2828
#方法前面的A_前缀是为了方法自省排序的时候使用
2929

3030
prodikaPath = r'd:\WorkProject\private\lqian\v6.1.1.0_20120724\Prodika'
31+
prodikaPath6103 = r'd:\WorkProject\private\lqian\v6.1.0.3\Prodika'
3132

3233

3334
def _ChangeCoreAppSettingValue(self,v):
@@ -65,13 +66,21 @@ def A_UnEnableControlDetailPath(self):
6566
self._ChangeCoreAppSettingValue(v)
6667

6768
def B_StartRemotingContainer(self):
68-
'''开启Remoting Container'''
69+
'''开启 611 Remoting Container'''
6970

7071
rPath = os.path.join(self.prodikaPath,r'Code\Apps\RemotingContainer\bin\RemotingContainer.exe -normal')
7172
GreenPrint('正在启动 Remoting Container...')
7273
#subprocess.Popen(args=rPath, shell=False)
7374
os.system(rPath)
7475

76+
def B_StartRemotingContainer6103(self):
77+
'''开启 6103 Remoting Container'''
78+
79+
rPath = os.path.join(self.prodikaPath6103,r'Code\Apps\RemotingContainer\bin\RemotingContainer.exe -normal')
80+
GreenPrint('正在启动 Remoting Container...')
81+
#subprocess.Popen(args=rPath, shell=False)
82+
os.system(rPath)
83+
7584
def C_LoadProdikaEnvironment(self):
7685
'''LoadEnv for prodika'''
7786
GreenPrint('正在加载环境变量...')

Utility/HttpWrapper-UnitTest.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python
2+
# -*- coding: UTF-8 -*-
3+
import unittest
4+
from HttpWrapper import HttpWrapper,HttpWrapperException
5+
6+
proxies = {'http':'10.182.46.232:80','https':'10.182.45.231:80'}
7+
8+
class HttpWrapperTestCase(unittest.TestCase):
9+
def PageNotFind(self):
10+
r = HttpWrapper('http://www.cnblogs.com/scottqiantest',enableProxy = True,proxyDict = proxies)
11+
assert r.GetResponseCode() == 404
12+
#print r.GetContent().decode('utf-8').encode("GB18030") #encode to GB18030 for displaying in cmd window
13+
#print r.GetHeaderInfo()
14+
#with self.assertRaises(HttpWrapperException):
15+
#r.GetContent()
16+
17+
def CorrectRequest(self):
18+
r = HttpWrapper('http://www.cnblogs.com')
19+
assert r.GetResponseCode() == 200
20+
21+
def PostDataRequest(self):
22+
data = {'tbUserName' : '1',
23+
'tbPassword' : '1',
24+
'__EVENTTARGET' : 'btnLogin',
25+
'__EVENTARGUMENT' : '',
26+
'__VIEWSTATE' : '/wEPDwULLTE1MzYzODg2NzZkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQtjaGtSZW1lbWJlcm1QYDyKKI9af4b67Mzq2xFaL9Bt',
27+
'__EVENTVALIDATION' : '/wEWBQLWwpqPDQLyj/OQAgK3jsrkBALR55GJDgKC3IeGDE1m7t2mGlasoP1Hd9hLaFoI2G05'}
28+
r = HttpWrapper('http://passport.cnblogs.com/login.aspx',data)
29+
assert r.GetContent().decode('utf-8').find(u'用户不存在') > 0
30+
31+
def RefererRequest(self):
32+
#tell server I'm from baidu.com
33+
r = HttpWrapper('http://www.stardrifter.org/cgi-bin/ref.cgi',referer='http://www.baidu.com')
34+
assert r.GetResponseCode() == 200
35+
assert r.GetContent().find(u'www.baidu.com') > 0
36+
37+
def AutoRedirectRequest(self):
38+
#auto redirect is enabled by default in HttpWrapper
39+
r = HttpWrapper('http://jigsaw.w3.org/HTTP/300/301.html',enableAutoRedirect = False)
40+
print r.GetResponseCode()
41+
assert r.GetUrl() == 'http://jigsaw.w3.org/HTTP/300/Overview.html'
42+
43+
#r = HttpWrapper('http://jigsaw.w3.org/HTTP/300/302.html')
44+
#assert r.GetUrl() == 'http://jigsaw.w3.org/HTTP/300/Overview.html'
45+
46+
def suite():
47+
suite = unittest.TestSuite()
48+
suite.addTest(HttpWrapperTestCase('PageNotFind'))
49+
#suite.addTest(HttpWrapperTestCase('CorrectRequest'))
50+
#suite.addTest(HttpWrapperTestCase('PostDataRequest'))
51+
#suite.addTest(HttpWrapperTestCase('RefererRequest'))
52+
#suite.addTest(HttpWrapperTestCase('AutoRedirectRequest'))
53+
return suite
54+
55+
if __name__ == '__main__':
56+
unittest.main(defaultTest = 'suite')

Utility/HttpWrapper.py

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#!/usr/bin/env python
2+
# -*- coding: UTF-8 -*-
3+
import urllib,urllib2,cookielib,zlib
4+
from gzip import GzipFile
5+
from StringIO import StringIO
6+
import sys
7+
8+
class HttpWrapperException(Exception):
9+
"""
10+
Custom HttpWrapper Exception
11+
"""
12+
pass
13+
14+
class HttpWrapper:
15+
"""
16+
A wrapper of http Request, integrated with cookie handler and smart referer handler
17+
18+
Usage:
19+
HttpWrapper('http://xxx.com',data=dict)
20+
"""
21+
22+
def __init__(self, url, postData=None,enableAutoRedirect = True,enableProxy = False,proxyDict = None,**header):
23+
"""
24+
url : url you want to request
25+
postData : data you want to post to the server, must be dict type,like {data1 : 'data'}
26+
enableAutoRedirect : auto redirect when server return 301,302 error
27+
enableProxy : whether enable proxy
28+
ProxyDict : proxy info. e.g ProxyDict = {'http':'10.182.45.231:80','https':'10.182.45.231:80'}
29+
**header : other request info. e.g. referer='www.sina.com.cn'
30+
"""
31+
32+
self.url = url
33+
self.postData = postData
34+
self.enableAutoRedirect = enableAutoRedirect
35+
self.enableProxy = enableProxy
36+
self.ProxyDict = proxyDict
37+
#tell server where i'm from,some explain about referer http://www.fwolf.com/blog/post/320
38+
if 'referer' in header:
39+
self.referer = header['referer']
40+
else:
41+
self.referer = None
42+
43+
if 'user-agent' in header:
44+
self.user_agent = header['user-agent']
45+
else:
46+
self.user_agent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16'
47+
48+
self.__SetupHandler()
49+
self.__SendRequest(self.__SetupRequest())
50+
51+
class ContentEncodingProcessor(urllib2.BaseHandler):
52+
"""
53+
A handler to add gzip capabilities to urllib2 requests
54+
"""
55+
56+
# add headers to requests
57+
def http_request(self, req):
58+
req.add_header("Accept-Encoding", "gzip,deflate")
59+
return req
60+
61+
# decode
62+
def http_response(self, req, resp):
63+
old_resp = resp
64+
# gzip
65+
if resp.headers.get("content-encoding") == "gzip":
66+
gz = GzipFile( fileobj = StringIO(resp.read()), mode="r")
67+
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
68+
resp.msg = old_resp.msg
69+
# deflate
70+
if resp.headers.get("content-encoding") == "deflate":
71+
gz = StringIO(self.deflate(resp.read()) )
72+
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code) # 'class to add info() and
73+
resp.msg = old_resp.msg
74+
return resp
75+
76+
def deflate(self,data): # zlib only provides the zlib compress format, not the deflate format;
77+
try: # so on top of all there's this workaround:
78+
return zlib.decompress(data, -zlib.MAX_WBITS)
79+
except zlib.error:
80+
return zlib.decompress(data)
81+
82+
83+
def __SetupHandler(self):
84+
"""
85+
setup cookie handler, proxy handler and redirect hander for urllib2 library
86+
"""
87+
cj = cookielib.CookieJar()
88+
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
89+
opener.addheaders = [('User-agent', self.user_agent)]
90+
if self.enableAutoRedirect == True:
91+
opener.add_handler(urllib2.HTTPRedirectHandler())
92+
if self.enableProxy == True:
93+
if self.proxyDict == None:
94+
raise HttpWrapperException('you must specify proxyDict when enabled Proxy')
95+
opener.add_handler(urllib2.ProxyHandler(self.proxyDict))
96+
urllib2.install_opener(opener)
97+
98+
def __SetupRequest(self):
99+
"""
100+
setup request information
101+
"""
102+
if self.url is None or self.url == '':
103+
raise HttpWrapperException("url can't be empty!")
104+
105+
if self.postData is not None:
106+
req = urllib2.Request(self.url, urllib.urlencode(self.postData))
107+
else:
108+
req = urllib2.Request(self.url)
109+
110+
if self.referer:
111+
req.add_header('referer', self.referer)
112+
113+
if self.user_agent:
114+
req.add_header('user-agent', self.user_agent)
115+
116+
return req
117+
118+
def __SendRequest(self,req):
119+
try:
120+
res = urllib2.urlopen(req)
121+
self.source = res.read()
122+
self.code = res.getcode()
123+
#get real url, if we have 301 redirect page
124+
self.url = res.geturl()
125+
self.head_dict = res.info().dict
126+
res.close()
127+
except urllib2.HTTPError,e:
128+
self.code = e.code
129+
self.source = e.fp.read()
130+
self.head_dict = e.headers
131+
#print e.code
132+
#print e.msg
133+
#print e.headers
134+
#print e.fp.read()
135+
#print u"error happended:\r\n Location: HttpWrapper.__SendRequest \r\n Error Information:", sys.exc_info()[1]
136+
137+
138+
def GetResponseCode(self):
139+
"""
140+
get response code, e.g. 200 or 302
141+
"""
142+
if self.code:
143+
return self.code
144+
return -1
145+
146+
def GetUrl(self):
147+
if self.url:
148+
return self.url
149+
return None
150+
151+
def GetContent(self):
152+
"""
153+
get content body of the response.
154+
usually, you need to decode those content. for example, GetContent().decode('utf-8')
155+
"""
156+
if "source" in dir(self):
157+
return self.source
158+
else:
159+
raise HttpWrapperException(u'HttpWrapper error happended:\r\n Location: HttpWrapper.GetContent \r\n Error Information:no content find')
160+
161+
def GetHeaderInfo(self):
162+
return self.head_dict
163+
164+
def GetCookie(self):
165+
if 'set-cookie' in self.head_dict:
166+
return self.head_dict['set-cookie']
167+
else:
168+
return None
169+
170+
def GetContentType(self):
171+
if 'content-type' in self.head_dict:
172+
return self.head_dict['content-type']
173+
else:
174+
return None
175+
176+
def GetCharset(self):
177+
contentType = self.GetContentType()
178+
if contentType is not None:
179+
index = contentType.find("charset")
180+
if index > 0:
181+
return contentType[index+8:]
182+
return None
183+
184+
def GetExpiresTime(self):
185+
if 'expires' in self.head_dict:
186+
return self.head_dict['expires']
187+
else:
188+
return None
189+
190+
def GetServerName(self):
191+
if 'server' in self.head_dict:
192+
return self.head_dict['server']
193+
else:
194+
return None
195+
196+
if __name__ == '__main__':
197+
b = HttpWrapper('http://www.sina.com/test/fe')
198+
print b.GetContent().decode('utf-8')
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)