当前位置: 首页 >服务端 > python 3 处理HTTP 请求的包

python 3 处理HTTP 请求的包

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 which implement the client side of the HTTP and HTTPS protocols. It is normally not used 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 retu entire content.

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>>> with urllib.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')>>> with urllib.request.urlopen("http://requestb.in/xrbl82xr", data) as f:... print(f.read().decode('utf-8'))

urllib3

urllib3:https://pypi.python.org/pypi/urllib3

urllib3 brings many critical features that are missing from the Python standard libraries:-Thread safety.-Connection pooling.-Client-side SSL/TLS verification.-File uploads with multipart encoding.-Helpers for retrying requests and dealing with HTTP redirects.-Support for gzip and 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.status200>>> r.data'User-agent: *\nDisallow: /deny\n'

Requests

Requests:http://docs.python-requests.org/en/latest/index.html

Requests 基于urllib3,号称“Requests is an elegant and simple HTTP library for Python, built for human beings.”,意思就是专门为人类设计的HTTP库。

使用的感觉就是优雅、简单大方 。推荐使用这个库,非常好用。

官方示例:

>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))>>> r.status_code200>>> r.headers['content-type']'application/json; charset=utf8'>>> r.encoding'utf-8'>>> r.textu'{"type":"User"...'>>> r.json(){u'private_gists': 419, u'total_private_repos': 77, ...}

总结

Python 3 处理HTTP请求的包:httpurlliburllib3requests

其中,http 比较 low-level,一般不直接使用。

urllib更 high-level一点,属于标准库。urllib3跟urllib类似,拥有一些重要特性而且易于使用,但是属于扩展库,需要安装。

requests 基于urllib3 ,也不是标准库,但是使用非常方便。

个人感觉,如果非要用标准库,就使用urllib。如果没有限制,就用requests。

作者:微微微笑
来源链接:https://www.cnblogs.com/miniren/p/5885393.html

版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaclub.cn/server/112987.html

标签:HTTP
分享给朋友:

“python 3 处理HTTP 请求的包” 的相关文章