专栏首页授客的专栏Python 基于urllib.request封装http协议类

Python 基于urllib.request封装http协议类

测试环境: 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,部分内容如下: \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: 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'}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Python 基于Python实现邮件发送

    注:需要修改mimetypes.py文件(该文件可通过文章底部的网盘分享连接获取),否则会报错,类似如下

    授客
  • Vue 修改网页标题和图标

    把要修改的图片放static目录下(注意:图片不能放src目录下,亲测无效),然后添加<Link>元素

    授客
  • Linux profile1,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part1

    profile,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part 1

    授客
  • 记一次在线跨集群迁移ES数据

    业务所有的服务器日志都是通过filebeat进行采集,然后写入到一个公共的ES集群中。因为当前使用的集群无法继续扩容了并且版本也较低(5.6.4), 所以需要把...

    bellen
  • [PHP]PHP rpc框架hprose测试

    陶士涵
  • android之json数据过长打印不全问题的解决

    在logcat中打后台打印json数据时,json数据太过于长就会有打印不全的问题,这里记录一个办法就是分段打印:

    砸漏
  • JcJc人工智能错别字校对系统API(网站错别字检查)

    JcJc人工智能错别字校对系统API - 网站错别字检查 中文校对软件 http://cuobiezi.net/zigen_jiaodui.html App...

    田春峰-JCJC错别字检测
  • Call to undefined function bcmath()的解决方法

    一查得知: php configure 时忘记加了 --enable-bcmath 参数.

    双面人
  • JcJc人工智能错别字校对系统API(网站错别字检查)

    中文校对软件 http://cuobiezi.net/zigen_jiaodui.html

    田春峰-JCJC错别字检测
  • iOS UITableView 滑动到底部加载更多数据

    很多APP都是滑动到底部时点击加载更多才会加载数据,这样用户体验就会有间断感,所以我们想用户看到最后时自动加载数据 怎么做呢

    剑行者

扫码关注云+社区

领取腾讯云代金券