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

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

基于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)


 

说明:

1params = 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'}

目录
相关文章
|
23天前
|
网络协议 Linux iOS开发
推荐:实现RTSP/RTMP/HLS/HTTP协议的轻量级流媒体框架,支持大并发连接请求
推荐:实现RTSP/RTMP/HLS/HTTP协议的轻量级流媒体框架,支持大并发连接请求
51 1
|
1天前
|
Python
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
|
1天前
|
JSON 测试技术 API
Python的Api自动化测试使用HTTP客户端库发送请求
【4月更文挑战第18天】在Python中进行HTTP请求和API自动化测试有多个库可选:1) `requests`是最流行的选择,支持多种请求方法和内置JSON解析;2) `http.client`是标准库的一部分,适合需要低级别控制的用户;3) `urllib`提供URL操作,适用于复杂请求;4) `httpx`拥有类似`requests`的API,提供现代特性和异步支持。根据具体需求选择,如多数情况`requests`已足够。
8 3
|
2天前
|
Python
Python 一步一步教你用pyglet制作可播放音乐的扬声器类
Python 一步一步教你用pyglet制作可播放音乐的扬声器类
14 0
|
7天前
|
缓存 网络协议
【计算机协议】第一章——HTTP协议详解
【计算机协议】第一章——HTTP协议详解
|
8天前
|
网络协议 Java API
深度剖析:Java网络编程中的TCP/IP与HTTP协议实践
【4月更文挑战第17天】Java网络编程重在TCP/IP和HTTP协议的应用。TCP提供可靠数据传输,通过Socket和ServerSocket实现;HTTP用于Web服务,常借助HttpURLConnection或Apache HttpClient。两者结合,构成网络服务基础。Java有多种高级API和框架(如Netty、Spring Boot)简化开发,助力高效、高并发的网络通信。
|
9天前
|
Python
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
52 0
|
9天前
|
索引 Python
python 格式化、set类型和class类基础知识练习(上)
python 格式化、set类型和class类基础知识练习
32 0
|
10天前
|
Python
python学习12-类对象和实例对象
python学习12-类对象和实例对象
|
11天前
|
缓存 安全 网络协议
Http协议是什么
【4月更文挑战第12天】HTTP是用于从WWW服务器传输超文本到浏览器的协议,基于TCP/IP,特点包括无连接、无状态、面向对象、无阻塞和可缓存。它的工作原理是客户端发送请求,服务器处理后返回响应。自1989年创建以来,HTTP已发展支持多媒体内容传输,并通过HTTPS提供安全保护。学习更多可参考计算机网络技术文献。
18 6