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")
>>> 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; '>>> r.encoding'utf-8'>>> r.text