python中request内容_Python 基于urllib.request封装http协议类

基于urllib.request封装http协议类

by:授客QQ:1033553122

测试环境:

Python版本:Python 3.3

代码实践

#!/usr/bin/env python

# -*- coding:utf-8 -*-

__author__ ='shouke'

importurllib.request

importhttp.cookiejar

importurllib.parse

classMyHttp:

'''配置要测试请求服务器的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)

defset_host(self, host):

self.host = host

defget_host(self):

returnself.host

defget_protocol(self):

returnself.protocol

defset_port(self, port):

self.port = port

defget_port(self):

returnself.port

#设置http头

defset_header(self, headers):

self.headers = headers

#封装HTTP GET请求方法

defget(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()

returnresponse

exceptException ase:

print('发送请求失败,原因:%s'% e)

return None

#封装HTTP POST请求方法

defpost(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()

returnresponse

exceptException ase:

print('发送请求失败,原因:%s'% e)

return None

#封装HTTP xxx请求方法

#自由扩展

案例1:

#!/usr/bin/env python

# -*- coding:utf-8 -*-

__author__ ='shouke'

fromhttpprotocolimportMyHttp

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'

fromhttpprotocolimportMyHttp

if__name__ =='__main__':

http = MyHttp('http','www.webxml.com.cn',80)#

header = {'Content-Type':'text/xml','charset':'utf-8'}

http.set_header(header)

params ='''

xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:web="http://WebXml.com.cn/">

'''

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,部分内容如下:

\xe7\x9b\xb4\xe8\xbe\x96\xe5\xb8\x82\xe7\x89\xb9\xe5\x88\xab\xe8\xa1\x8c\xe6\x94\xbf\xe5\x8c\xba……

如上,返回十六进制(\x表示16进制)的字符e7,9b等

解决方法:输出前先解码,如下

response = response.decode('utf-8')

print(response)

解码后的输出结果:

直辖市特别行政区……

案例3:

importjson

fromhttpprotocolimportMyHttp

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'}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值