Python urllib3軟件包的使用說明
urllib3是一款Python 3的HTTP客戶端。
Python標(biāo)準(zhǔn)庫提供了urllib。在Python 2中,另外提供了urllib2;而在Python 3中,重構(gòu)了urllib和urllib2到標(biāo)準(zhǔn)庫urllib,并另外提供了urllib3。
1. urllib3的特性
線程安全
連接緩沖池
客戶端SSL/TLS驗證
文件上傳
請求重試
HTTP重定向
支持gzip和deflate encoding
支持HTTP和SOCKS的代理
2. 安裝
urllib3不是Python 3的標(biāo)準(zhǔn)庫,要使用需要另外安裝,pip命令如下:
pip install urllib3
3. 用法
1) HTTP GET請求
>>> import urllib3 >>> http = urllib3.PoolManager() >>> r = http.request('GET', 'http://httpbin.org/robots.txt') >>> r.status 200 >>> r.data ... >>> r.headers ...
注意:任何HTTP請求,只有通過PoolManager對象發(fā)出,才能夠提供連接緩沖池和線程安全特性。
任何請求的返回對象都是HTTPResponse對象,其中包含status, data和headers三個屬性。
2) HTTP POST請求
>>> import urllib3 >>> http = urllib3.PoolManager() >>> r = http.request('POST', 'http://httpbin.org/post', fields={'hello': 'Xiangbin'}) >>> r.status 200 >>> r.data ... >>> r.headers ...
3) JSON響應(yīng)的處理
>>> import urllib3 >>> import json >>> http = urllib3.PoolManager() >>> r = http.request('GET', 'http://httpbin.org/ip') >>> r.data b'{\n "origin": "10.23.1.37"\n}\n' >>> json.loads(r.data.decode('utf-8')) {'origin': '127.0.0.1'}
注意:使用json的loads()方法
4) 流式響應(yīng)的處理
>>> import urllib3 >>> http = urllib3.PoolManager() >>> r = http.request('GET', 'http://httpbin.org/bytes/1024', preload_content=False) >>> for chunk in r.stream(32): ... print(chunk) ... >>> r.release_conn()
注意:preload_content=False表示流式處理響應(yīng)數(shù)據(jù)。
處理stream()方法讀取響應(yīng)數(shù)據(jù)之外,還可以使用read()方法,示例如下:
>>> import urllib3 >>> http = urllib3.PoolManager() >>> r = http.request('GET', 'http://httpbin.org/bytes/1024', preload_content=False) >>> r.read(4) b'\x88\x1f\x8b\xe5' >>> r.release_conn()
5) 請求帶參數(shù)
>>> r = http.request('GET', 'http://httpbin.org/headers', fields={'hello': 'Xiangbin'}, headers={'X-Something': 'value'})
對于POST和PUT方法,需要將參數(shù)編碼后,這樣才可以追加到URL,示例如下:
>>> from urllib.parse import urlencode >>> encoded_args = urlencode({'arg': 'value'}) >>> url = 'http://httpbin.org/post?' + encoded_args >>> r = http.request('POST', url)
當(dāng)然,最好還是以fields參數(shù)形式,urllib3將自動編碼,示例如下:
>>> r = http.request('POST', 'http://httpbin.org/post', fields={'hello': 'Xiangbin'})
使用JSON模塊,還可以以body形式發(fā)送請求參數(shù),示例如下:
>>> import json >>> data = {'Hello': 'Xiangbin'} >>> encoded_data = json.dumps(data).encode('utf-8') >>> r = http.request('POST', 'http://httpbin.org/post', body=encoded_data, headers={'Content-Type': 'application/json'}) >>> json.loads(r.data.decode('utf-8'))['json'] {'Hello': 'Xiangbin'}
6) 上傳文件
文本文件
>>> with open('example.txt') as fp: ... file_data = fp.read() >>> r = http.request( ... 'POST', ... 'http://httpbin.org/post', ... fields={ ... 'filefield': ('example.txt', file_data, 'text/plain'), ... }) >>> json.loads(r.data.decode('utf-8'))['files'] {'filefield': '...'}
注意:上傳文件必須使用POST方法。
二進(jìn)制文件
>>> with open('example.jpg', 'rb') as fp: ... binary_data = fp.read() >>> r = http.request( ... 'POST', ... 'http://httpbin.org/post', ... body=binary_data, ... headers={'Content-Type': 'image/jpeg'}) >>> json.loads(r.data.decode('utf-8'))['data'] b'...'
補充知識:Python的requests軟件包詳解
requests是一款Python的第三方HTTP類庫,便于進(jìn)行HTTP訪問。
1. requests的特性
能夠發(fā)送HTTP 1.1請求
無需手工為GET方法設(shè)置URL的請求參數(shù),無需手工為POST方法組編碼表單形式
借助于urllib3實現(xiàn)HTTP請求的連接會話緩存
支持Python 2.6, 2.7, 3.3-3.7
2. requests的安裝
requests不是Python標(biāo)準(zhǔn)庫,需要使用PIP安裝,命令如下:
pip install requests
安裝過程如下:
C:\Sam\works>pip install requests Collecting requests Downloading https://files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl (57kB) 100% |████████████████████████████████| 61kB 17kB/s Collecting certifi>=2017.4.17 (from requests) Downloading https://files.pythonhosted.org/packages/18/b0/8146a4f8dd402f60744fa380bc73ca47303cccf8b9190fd16a827281eac2/certifi-2019.9.11-py2.py3-none-any.whl (154kB) 100% |████████████████████████████████| 163kB 18kB/s Collecting idna<2.9,>=2.5 (from requests) Downloading https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl (58kB) 100% |████████████████████████████████| 61kB 10kB/s Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 (from requests) Downloading https://files.pythonhosted.org/packages/e0/da/55f51ea951e1b7c63a579c09dd7db825bb730ec1fe9c0180fc77bfb31448/urllib3-1.25.6-py2.py3-none-any.whl (125kB) 100% |████████████████████████████████| 133kB 32kB/s Collecting chardet<3.1.0,>=3.0.2 (from requests) Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB) 100% |████████████████████████████████| 143kB 48kB/s Installing collected packages: certifi, idna, urllib3, chardet, requests Successfully installed certifi-2019.9.11 chardet-3.0.4 idna-2.8 requests-2.22.0 urllib3-1.25.6 You are using pip version 19.0.3, however version 19.3.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
3. requests的接口
1) Main interfaces
requests.request() requests.head() requests.get('url', params={'key1':'value1', 'key2':'value2'},headers={'user-agent': '...'}, cookies={'name1':'value2'}) requests.post('url', data={'key':'value'}) requests.post('url', json={'key':'value'}) requests.post('url', files={'uploaded_file': open('report.xls', 'rb')}) requests.post('url', files={'uploaded_file': ('report.xls', open('report.xls', 'rb'), 'application/excel', {'Expires': '0'})}) requests.post('url', files={'uploaded_file': ('temp.txt', 'one line\ntwo lines\n')}) requests.put('url', data={'key':'value'}) requests.patch() requests.delete('url') def getGithub(): github_url = 'https://api.github.com/user/repos' myresponse = requests.get(github_url, auth=('champagne', 'myPassword')) print(myresponse.json()) def postGithub(): github_url = 'https://api.github.com/user/repos' data = json.dumps({'name':'python test', 'description':'a python test repo'}) myresponse = requests.post(github_url, data, auth=('champagne', 'myPassword')) print(myresponse.text)
2) requests.Session類
import requests
requests.Session()
3) requests.Request類
import requests
requests.Request('GET', 'http://httpbin.org/get')
4) requests.PreparedRequest類
import requests req = requests.Request('GET', 'http://httpbin.org/get') preq = req.prepare()
5) requests.Response類
import requests r = requests.get('https://api.github.com/events') r.headers['content-type'] #'application/json;charset=utf8' r.url r.status_code #200==requests.codes.ok r.encoding #'utf-8' by default r.raw #raw content r.text #text content r.content #binary content r.json()#json content, recommended r.cookies['a_key']
注意:調(diào)用json()方法,如果返回結(jié)果不是有效的JSON數(shù)據(jù),則拋出ValueError異常。
6) requests.adapters.BaseAdapter類
7) requests.adapters.HTTPAdapter類
requests提供的使用urllib3的HTTP Adapter
以上這篇Python urllib3軟件包的使用說明就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python將logging模塊封裝成單獨模塊并實現(xiàn)動態(tài)切換Level方式
這篇文章主要介紹了python將logging模塊封裝成單獨模塊并實現(xiàn)動態(tài)切換Level方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05Python實現(xiàn)將通信達(dá).day文件讀取為DataFrame
今天小編就為大家分享一篇Python實現(xiàn)將通信達(dá).day文件讀取為DataFrame,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12Python中WatchDog的使用經(jīng)驗總結(jié)
在?python?中文件監(jiān)視主要有兩個庫,一個是?pyinotify,一個是?watchdog,本文主要為大家詳細(xì)介紹一下Python中WatchDog的使用相關(guān)經(jīng)驗,感興趣的小伙伴可以了解下2023-12-12關(guān)于Python中flask-httpauth庫用法詳解
這篇文章主要介紹了關(guān)于Python中flask-httpauth庫用法詳解,Flask-HTTPAuth是一個?Flask?擴展,它簡化了?HTTP?身份驗證與?Flask?路由的使用,需要的朋友可以參考下2023-04-04Pandas之Fillna填充缺失數(shù)據(jù)的方法
這篇文章主要介紹了Pandas之Fillna填充缺失數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06