基于urllib.request封装http协议类
by:授客QQ:1033553122
测试环境:
Python版本:Python 3.3
代码实践
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
import urllib.request
import http.cookiejar
import urllib.parse
class MyHttp:
'''配置要测试请求服务器的ip、端口、域名等信息,封装http请求方法,http头设置'''
def __init__(self, protocol, host, port, header = {}):
# 从配置文件中读取接口服务器IP、域名,端口
self.protocol = protocol
self.host = host
self.port = port
self.headers = header # http 头
#install cookie #自动管理cookie
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
urllib.request.install_opener(opener)
def set_host(self, host):
self.host = host
def get_host(self):
return self.host
def get_protocol(self):
return self.protocol
def set_port(self, port):
self.port = port
def get_port(self):
return self.port
# 设置http头
def set_header(self, headers):
self.headers = headers
# 封装HTTP GET请求方法
def get(self, url, params=''):
url = self.protocol + '://' + self.host + ':' + str(self.port) + url + params
print('发起的请求为:%s' % url)
request = urllib.request.Request(url, headers=self.headers)
try:
response = urllib.request.urlopen(request)
response = response.read()
return response
except Exception as e:
print('发送请求失败,原因:%s' % e)
return None
# 封装HTTP POST请求方法
def post(self, url, data=''):
url = self.protocol + '://' + self.host + ':' + str(self.port) + url
print('发起的请求为:%s' % url)
request = urllib.request.Request(url, headers=self.headers)
try:
response = urllib.request.urlopen(request, data)
response = response.read()
return response
except Exception as e:
print('发送请求失败,原因:%s' % e)
return None
# 封装HTTP xxx请求方法
# 自由扩展
案例1:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
from httpprotocol import MyHttp
if __name__ == '__main__':
http = MyHttp('https', 'www.baifubao.com', 443)
params = {"cmd":1059,"callback":"phone", "phone":"15850781443"}
params = urllib.parse.urlencode(params)
response = http.get('/callback?', params)
print(response)
输出response内容如下:
b'phone({"meta":{"result":"0","result_info":"","jump_url":""},"data":{"operator":"\u79fb\u52a8","area":"\u6c5f\u82cf","area_operator":"\u6c5f\u82cf\u79fb\u52a8","support_price":{"100":"115","500":"507","1000":"1000","2000":"2000","3000":"2996","5000":"4994","10000":"9989","20000":"19979","30000":"29969","50000":"49948"}}})'
如上,返回Unicode编码的数据:“"\u79fb\u52a8",……”,
解决方法:输出前先解码,如下
response = response.decode('unicode_escape')
print(response)
解码后的输出如下:
phone({"meta":{"result":"0","result_info":"","jump_url":""},"data":{"operator":"移动","area":"江苏","area_operator":"江苏移动","support_price":{"100":"115","500":"507","1000":"1000","2000":"2000","3000":"2996","5000":"4994","10000":"9989","20000":"19979","30000":"29969","50000":"49948"}}})
案例2:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
from httpprotocol import MyHttp
if __name__ == '__main__':
http = MyHttp('http', 'www.webxml.com.cn', 80) #
header = {'Content-Type':'text/xml','charset':'utf-8'}
http.set_header(header)
params = '''<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://WebXml.com.cn/">
<soapenv:Header/>
<soapenv:Body>
<web:getSupportProvince/>
</soapenv:Body>
</soapenv:Envelope>'''
params = params.encode(encoding='UTF-8')
response = http.post('/WebServices/WeatherWebService.asmx?', params)
print(response)
说明:
1、params = params.encode(encoding='UTF-8') # 如果未添加该行代码,会报错如下:
POST data should be bytes or an iterable of bytes. It cannot be of type str.
2、
header = {'Content-Type':'text/xml','charset':'utf-8'}
http.set_header(header)
以上两行代码,为请求添加请求头,如果未添加,则会报错,如下:
HTTP Error 415: Unsupported Media Type
3、输出response,部分内容如下:
xe7x9bxb4xe8xbex96xe5xb8x82xe7x89xb9xe5x88xabxe8xa1x8cxe6x94xbfxe5x8cxba……
如上,返回十六进制(x表示16进制)的字符e7,9b等
解决方法:输出前先解码,如下
response = response.decode('utf-8')
print(response)
解码后的输出结果:
直辖市特别行政区……
案例3:
import json
from httpprotocol import MyHttp
if __name__ == '__main__':
http = MyHttp('http', 'info.so.360.cn', 80)
header = {'Content-Type':'application/x-www-form-urlencoded','charset':'utf-8'}
http = MyHttp('http', 'info.so.360.cn', 80)
http.set_header(header)
url = '/index.php?g=Embody&m=Index&a=submit'
parmas = '{"websitetype":"博客论坛","url":"http://blog.sina.com.cn/ishouke","email":"1033553122@40qq.com","checkcode":"rkqj"}'
parmas = parmas.encode('utf-8')
response = http.post(url,parmas)
print(response.decode('utf-8'))
说明:如果服务器支持的内容类型(‘Content-Type’)为json则要修改请求头,如下
header = {'Content-Type':'application/json','charset':'utf-8'}