参考链接:https://urllib3.readthedocs.io/en/latest/user-guide.html#using-timeouts
首先我们肯定要安装urllib3这个库了(python3支持)
pip install urllib3
然后我们在编写任何脚本之前需要导入这个模块
import urllib3
现在我们需要一个PoolManager去建立一个request,这个的connection pooling的作用是为了thread safety
语句为:http=urllib3.PoolManager()
现在我们尝试一下get请求
import urllib3
http=urllib3.PoolManager()
r=http.request('GET','http://httpbin.org/robots.txt')
print(r.data)
>>b'User-agent: *\nDisallow: /deny\n'
request()返回一个HTTPResponse对象,我们可以使用request()对任何HTTP verb做请求
如下所示:post请求
import urllib3
http=urllib3.PoolManager()
r=http.request('POST','http://httpbin.org/post',fields={'hello':'world'})
0X02:
Response content
这个HTTPResponse对象提供了status,data,headers等属性
import urllib3
http=urllib3.PoolManager()
r=http.request('GET','http://httpbin.org/ip')
print(r.status)
print(r.data)
print(r.headers)
200
b'{\n "origin": "120.203.18.108"\n}\n'
HTTPHeaderDict({'Connection': 'keep-alive', 'Server': 'gunicorn/19.7.1', 'Date': 'Thu, 03 May 2018 06:50:51 GMT', 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'X-Powered-By': 'Flask', 'X-Processed-Time': '0', 'Content-Length': '33', 'Via': '1.1 vegur'})
0x03:Json content
json文本在加载之前需要进行一次utf-8解密,将这个data进行一次反序列化,当然记得导入json模块
import json
r=http.request('GET','http://httpbin.org/ip')
print(json.loads(r.data.decode('utf-8')))
{'origin': '120.203.18.108'}
0x04:Binary content
data响应的属性始终设置为表示响应内容的字节字符串
r=http.request('GET','http://httpbin.org/bytes/8')
print(r.data)
b';P\x8aq\x97uO.'
注意:对于很大的response,我们有的时候需要去stream这个response
Streaming and IO
我们设置preload_content to False是为了urllib3能够stream这个请求文本,streamI()能够让我们迭代响应内容文本
注意:我们在使用preload_content=False的时候,我们应该调用release_conn()将http连接释放
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()
我们也可以将HTTPResponse实例视为文件类对象,这样的话可以让我们做下缓冲
import urllib3
http=urllib3.PoolManager()r=http.request('GET','http://httpbin.org/bytes/1024',preload_content=False)
r.read()
Calls toread将阻塞,直到有更多的相应的数据可用
r=http.request('GET','http://httpbin.org/bytes/1024',preload_content=False)
reader=io.BufferedReader(r,8)
print(reader.read(4))
r.release_conn()
b'\x959\xc3\x10'
我们也可以使用类文件对象来执行诸如codecs以下内容解码内容的操作
import codecs
reader=codecs.getreader('utf-8')
r=http.request('GET','http://httpbin.org/ip',preload_content=False)
print(json.load(reader(r)))
>>{'origin': '120.203.18.108'}
0x05:Request data
Headers
我们可以在headers参数中指定标题作为字典request()
import urllib3
http=urllib3.PoolManager()
r=http.request('GET','http://httpbin.org/headers',headers={'X-Something':'value'})
print(json.loads(r.data.decode('utf-8'))['headers'])
>>{'Accept-Encoding': 'identity', 'Connection': 'close', 'Host': 'httpbin.org', 'X-Something': 'value'}
Query parameters
对于get,head和delete请求,我们可以简单的传递参数作为一个字典fields参数request()
r=http.request('GET','http://httpbin.org/get',fields={'arg':'value'})
print(json.loads(r.data.decode('utf-8'))['args'])
>>{'arg': 'value'}
对于post和put请求,我们需要在url中手动编码查询参数
import urllib3
from urllib.parse import urlencode
encoded_args=urlencode({'arg':'value'})
url='http://httpbin.org/post?'+encoded_args
r=http.request('POST','url')
print(json.loads(r.data.decode('utf-8'))['args'])
Form data
对于put和post请求,urllib3将会自动的对表单提供给参数的字典进行编码
r=http.request('POST','http://httpbin.org/post',fields={'field':'value'})
print(json.loads(r.data.decode('utf-8'))['form'])
>>{'field': 'value'}
Json
我们可以通过将编码指定为body参数并在Content-Type调用的时候设置标题,来向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'})
print(json.loads(r.data.decode('utf-8'))['json'])
>>{'attribute': 'value'}
Files&binary data
要使用multipart/form-data编码上传文件,可以使用与表单数据相同的方法,并将文件字段指定为一下元组(file_name,file_data)
with open('test.txt')as fp:
file_data=fp.read()
r=http.request('POST','http://httpbin.org/post',fields={'filefields':('test.txt',file_data)})
print(json.loads(r.data.decode('utf-8'))['files'])
尽管指定文件名不是严格要求的,但是为了匹配浏览器的行为,建议使用它。当然我们还可以传递元组的第三项来显式的指定文件的MIME类型
with open('test.txt')as fp:
file_data=fp.read()
r=http.request('POST','http://httpbin.org/post',fields={'filefields':('test.txt',file_data,'text/plain')})
print(json.loads(r.data.decode('utf-8'))['files'])
要发送原始的二进制数据,只需要指定body参数,还建议设置Content-Type标题
with open('test.jpg','rb')as fp:
binary_data=fp.read()
r=http.request('POST','http://httpbin.org/post',body=binary_data,headers={'Content-Type':'image/jpeg'})
print(json.loads(r.data.decode('utf-8'))['data'])
>>data:application/octet-stream;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCABIAGADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExB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UsqFeLE4tSlzR6HTSoy2OJ1Ga/1jxJfXmpRyrqN/O9zdB0KMZJCXJKnpndn6Gum8GeFWuJzIwCDuzd/YVPL9s8Za1LqV7sRpeAEBEcKj7saAkthRxkkn1JOTXbeF9Aaa1yiKyrwACRknsMcf5NeTVrdT2MNg7tXL/g2IaAsnlsg807jweldHp+pXuv6tbWFlHLcXl9KltBGgy0sjsFVQPUkgf8A1hXMX5ksZjvQggYPzd/T6f8A16+mf2Mfg8PBHhN/iVrUANxMrW/h63cYLbso1x/wL5lU9kEjchlNc8W27nbVSpqxvfG34P8Air4FeLbjRfF2jXejX6MfLd13xXag48yGQfI6dOVJxnBAIIHlfiW+MlnPGqttZSCWGckg/wCeKKK55RUaritkddCTqUlOW7PC/FPggROxNuYRIT+8HBP4D+n5iuT1bwxLCQ5QOpOBwf8AI/ziiiutyatYwdKLi2zNu/DLRuf3LMP723O3n8v5/nxVY+HHSXAtmXnJZlI/AUUVUaje5zyoxJYPDc6xBvICxg/xHBPr+HvW1oPhFJgHcA85I/hPt/j9PwooqJTdjejh4X1OntfC21lPmKE652kAD0z261s6OyaRFvG9SwIUY6j/ABJ/kKKK5pNvRnWoqK5kdl+zn8HX/aG+Mun6HKsq6ZETeapJGTmG0QjcAR0ZyViUjkM4PQHH158TPFEOv66kFkkUWk6YBbWcUK7YsKApZQOAoACKOm1cjhyKKK66KSVzzMU252P/2Q==
0x06:Certificate verfication
强烈建议始终使用ssl验证。默认情况下,urllib3不验证https请求
为了启用验证,我们需要一组根证书。最简单而且最可靠的方法就是使用火狐提供的根证书包的certifi包
pip install certifi
获得证书后,我们可以创建一个PoolManager在发出请求的时候验证证书的方法
http=urllib3.PoolManager(cer_reqs='CERT_REQUIRED',ca_certs=certifi.where())
这个PoolManager将会自动的处理证书的验证,并在SSLError再验证失败的时候提出
r=http.request('GET','https://google.com')
r=http.request('GET', 'https://expired.badssl.com')
注意:如果需要,你可以使用操作系统提供的证书。只需要指定证书包的完整路径作为ca_carts参数而不是certifi.where()。例如,大多数linux操作系统存储证书/etc/ssl/certs/ca-certificates.crt。其他的操作系统可能很难。
0x07:Using timeouts
超时允许你控制请求在被中止前运行多长时间。再简单情况下,你可以将超时指定为float to request()
http=urllib3.PoolManager()
r=http.request('GET','http://httpbin.org/delay/3',timeout=4.0)
print(r.status)
>>200
http=urllib3.PoolManager()
r=http.request('GET','http://httpbin.org/delay/3',timeout=2.5)
print(r.status)
>>ReadTimeoutError
对于更加精细的控制,你可以使用一个Timeout实例,让你指定单独的连接和读取超时
http=urllib3.PoolManager()
r=http.request('GET','http://httpbin.org/delay/3',timeout=urllib3.Timeout(connect=1.0))
http=urllib3.PoolManager()
r=http.request('GET','http://httpbin.org/delay/3',timeout=urllib3.Timeout(connect=1.0,read=2.0))
print(r.status)
如果你希望所有的请求都受到相同的超时时间限制的话,我们可以在PoolManager级别上指定超时值
http=urllib3.PoolManage(timeout=3.0)
http=urllib3.PoolManage(timeout=3.0,read=2.0)
当然我们任然可以指定timeout来覆盖这个PoolManager超时的request()
0x08:Retrying requests
urllib3可以自动的重试idempotent等请求。同样的机制可以处理重定向。你可以使用retries参数to来控制重试requests(),默认情况下,urllib3将重试请求3次冰球跟随最多3次重定向。要更改重试次数,只需要指定一个整数即可
http=urllib3.PoolManager()
r=http.request('GET','http://httpbin.org/ip',retries=10)
print(r.status)
为了能够取消所有的重试和重定向,可以使用retries=False
http=urllib3.PoolManager()
r=http.request('GET','http://httpbin.org/redirect/1',retries=False)
print(r.status)
>>302
要禁用重定向但是需要保留重试,请指定redirect=False
http=urllib3.PoolManager()
r=http.request('GET','http://httpbin.org/redirect/1',redirect=False)
print(r.status)
>>302
为了更细化的控制,你可以使用一个retry实例。这个类允许你更好的控制请求重试的方式
例如:总共执行3次重试,但仅限于2次重定向。
http=urllib3.PoolManager()
r=http.request('GET','http://httpbin.org/redirect/1',retries=urllib3.Retry(3,redirect=2))
print(r.status)
当然你也可以禁用太多的重定向的异常,并且返回302响应
http=urllib3.PoolManager()
r=http.request('GET','http://httpbin.org/redirect/1',retries=urllib3.Retry(redirect=2,raise_on_redirect=False))
print(r.status)
如果你希望所有的请求都遵循相同的重试策略,那门可以在PoolManager级别上指定重试。
http=urllib3.PoolManager(retries=False)
http=urllib3.PoolManager(retries=urllib3.retry(5,redirect=2))
当然我们同样是可以使用retries来覆盖这个PoolManager的重试策略request()
0x09:Errors&Exceptions
urllib3包装较低级别的异常,例如:
http=urllib3.PoolManager()
try:
r=http.request('GET', 'nx.example.com', retries=False)
print(r.status)
except urllib3.exceptions.NewConnectionError:
print("urllib3 connection error!")
0x010:Logging
如果你使用的是标准库logging模块,urllib3将会发出多个日志。在某些情况下,这不可能是不可取的。你可以使用标准记录器界面来更改urllib3记录器的日志级别
logging.getLogger('urllib3').setLevel(logging.WARNING)