python3处理http请求的包
来源:互联网 发布:高级算法工程师年薪 编辑:程序博客网 时间:2024/06/15 16:48
1.1. http
http: https://docs.python.org/3/library/http.html
http是一个包,里面含有多个模块:http.client,http.server,http.cookies,http.cookiejar。
http.client 对应python2.X的 httplib 模块。
官方文档对 http.client的说明如下:
This module defines classes whichimplement the client side of the HTTP and HTTPS protocols. It is normally notused directly — the moduleurllib.request uses it to handle URLs that use HTTP and HTTPS.
总结起来就是:该库一般不直接使用,比较底层。
GET的官方例子:
>>> import http.client
>>> conn =http.client.HTTPSConnection("www.python.org")
>>> conn.request("GET","/")
>>> r1 = conn.getresponse()
>>> print(r1.status, r1.reason)
200 OK
>>> data1 = r1.read() # This will return entire content.
2.2. urllib
urllib:https://docs.python.org/3/library/urllib.html
urllib也是一个包,里面含有多个模块:urllib.request,urllib.error,urllib.parse,urllib.robotparser。
这里的urllib.request跟python 2.X 的urllib2有点像。
urllib.request 基于http.client,但是比 http.client 更高层一些。
发送请求使用urllib.request.urlopen,URL可以接受字符串或者Request对象。带有data参数就是POST方法,否则就是GET。
GET:
>>> import urllib.request
>>> import urllib.parse
>>> params =urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> url ="http://www.musi-cal.com/cgi-bin/query?%s" % params
>>> withurllib.request.urlopen(url) as f:
... print(f.read().decode('utf-8'))
POST:
>>> import urllib.request
>>> import urllib.parse
>>> data =urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> data = data.encode('ascii')
>>> withurllib.request.urlopen("http://requestb.in/xrbl82xr", data) as f:
... print(f.read().decode('utf-8'))
3.3. urllib3
urllib3:https://pypi.python.org/pypi/urllib3
urllib3 brings many critical features thatare missingfrom the Python standard libraries:
-Thread safety.
-Connection pooling.
-Client-side SSL/TLS verification.
-File uploads with multipart encoding.
-Helpers for retrying requestsand dealing with HTTP redirects.
-Support for gzipand deflate encoding.
-Proxy support for HTTP and SOCKS.
-100% test coverage.
总结起来就是:相比python的标准库,urllib3有很多很重要的特性,比如线程安全等。
同时urllib3也很强大而且易于使用。
GET示例:
>>> import urllib3
>>> http = urllib3.PoolManager()
>>> r = http.request('GET','http://httpbin.org/robots.txt')
>>> r.status
200
>>> r.data
'User-agent: *\nDisallow: /deny\n'
4.4. Requests
Requests:http://docs.python-requests.org/en/latest/index.html
Requests 基于urllib3,号称“Requests is an elegant and simple HTTP library for Python, built forhuman beings.”,意思就是专门为人类设计的HTTP库。
使用的感觉就是优雅、简单大方 。推荐使用这个库,非常好用。
官方示例:
>>> r = requests.get('https://api.github.com/user', auth=('user','pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
u'{"type":"User"...'
>>> r.json()
{u'private_gists': 419, u'total_private_repos': 77, ...}
5.5. 总结
Python 3 处理HTTP请求的包:http,urllib,urllib3,requests。
其中,http比较 low-level,一般不直接使用。
urllib更high-level一点,属于标准库。urllib3跟urllib类似,拥有一些重要特性而且易于使用,但是属于扩展库,需要安装。
requests 基于urllib3,也不是标准库,但是使用非常方便。
个人感觉,如果非要用标准库,就使用urllib。如果没有限制,就用requests。
- python3处理http请求的包
- Python3处理HTTP请求
- Python3的两种HTTP请求实现
- Nginx的HTTP请求处理
- MFC 的 HTTP 请求处理
- http,https请求的处理
- python3 urllib包与http包的使用
- python3发起一个http请求
- 修改Apache的http请求包上限
- http请求包与请求返回的是什么
- 服务器端技术----Http请求的处理过程
- Scoket模拟HTTP请求响应的处理
- Handler处理HTTP请求的封装
- Apache处理http请求的生命周期
- 用于处理HTTP请求的工具类
- MFC 处理 HTTP 请求的基本方法
- Erlang cowboy 处理简单的HTTP请求
- MFC 处理 HTTP 请求的基本方法
- 匈牙利算法
- stm32 IO 设置
- 项目总结(三)——复制文件时显示进度对话框
- Comprehensive learning path – Data Science in Python
- Python 环境搭建
- python3处理http请求的包
- Java动态拼接SQL--03--JdbcTemple
- 小程序中点击事件传参
- PAT 甲级 1045. Favorite Color Stripe (30)
- Commom(十五)—— ShakeCamera摇晃摄像机
- 面试题:List、Map、Set三个接口,存取元素时,各有什么特点?
- 清单是如何读取的?
- 虚拟环境中Django使用python3完成模型类的迁移
- Python面向对象,子类,继承