創(chuàng)建請求
import urllib3
# 創(chuàng)建PoolManager 實(shí)例來處理http請求, 該poolmanager處理所有的連接池細(xì)節(jié)和線程安全事宜.
http = urllib3.PoolManager()
# request()返回一個(gè)HTTPResponse對象 .
r = http.request('GET', 'http://httpbin.org/robots.txt')
處理響應(yīng)內(nèi)容 Response content
HTTPResponse對象提供 status喝检、data 和 header屬性心软。
json內(nèi)容
data屬性可以被轉(zhuǎn)換為json對象 :
>>> import json
>>> r = http.request('GET', 'http://httpbin.org/ip')
>>> json.loads(r.data.decode('utf-8'))
{'origin': '127.0.0.1'}
http.request( method , url, fileds, headers , **kw)
cookie 設(shè)置 :
cookie 設(shè)置在headers中 , 如:
headers = {
'Cookie': 'JSESSIONID=scc7mqk00akn1jw6ih80la6re'
}
代理設(shè)置
直接使用 ProxyManager
>>> import urllib3
>>> proxy = urllib3.ProxyManager('http://localhost:3128/')
>>> proxy.request('GET', 'http://google.com/')
ProxyManager的使用同PoolManager .
SOCKS4 SOCKS5 代理
使用SOCKSProxyManager 來使用SOCKS代理.
需要安裝 PySocks 或 安裝帶socks的urllib3 , pip install urllib3[socks]
>>> from urllib3.contrib.socks import SOCKSProxyManager
>>> proxy = SOCKSProxyManager('socks5://localhost:8889/')
>>> proxy.request('GET', 'http://google.com/')
查詢參數(shù)
- 使用 GET HEAD DELETE請求方法時(shí), 直接將參數(shù)以字典的形式傳遞給fields:
>>> r = http.request(
... 'GET',
... 'http://httpbin.org/get',
... fields={'arg': 'value'})
>>> json.loads(r.data.decode('utf-8'))['args']
{'arg': '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)
>>> json.loads(r.data.decode('utf-8'))['args']
{'arg': 'value'}
表單數(shù)據(jù)
對于POST ,PUT請求, urllib3會(huì)自動(dòng)對字典數(shù)據(jù)進(jìn)行form-encode .
>>> r = http.request(
... 'POST',
... 'http://httpbin.org/post',
... fields={'field': 'value'})
>>> json.loads(r.data.decode('utf-8'))['form']
{'field': 'value'}
JSON
當(dāng)使用json數(shù)據(jù)時(shí), 需要指定headers的 Content-Type 為 application/json , 然后將dict編碼為json格式放到body字段:
>>> import json
>>> data = {'attribute': 'value'}
>>> 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']
{'attribute': 'value'}
文件 或二進(jìn)制數(shù)據(jù)
- 當(dāng)上傳文件時(shí), 使用multipart/form-data , 需要在上面上傳表單數(shù)據(jù)的基礎(chǔ)上加上(file_name, file_data) 元祖:
>>> with open('example.txt') as fp:
... file_data = fp.read()
>>> r = http.request(
... 'POST',
... 'http://httpbin.org/post',
... fields={
... 'filefield': ('example.txt', file_data),
... })
>>> json.loads(r.data.decode('utf-8'))['files']
{'filefield': '...'}
注意: 指定filenname 并不是必須的, 是瀏覽器建議使用的. 可以在元組中指定文件的MIME類型 .
>>> r = http.request(
... 'POST',
... 'http://httpbin.org/post',
... fields={
... 'filefield': ('example.txt', file_data, 'text/plain'),
... })
- 上傳二進(jìn)制數(shù)據(jù) , 傳遞body參數(shù) ,同時(shí)指定content-type
>>> 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'...'