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 return 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.text
u'{"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。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章