import requests
r = requests.get('https://www.baidu.com')
print(r.request.headers) # 请求头
print(r.headers) # 响应头
3.Prepared Requests
4.SSL证书验证
| 使用 | 说明 |
| verify=False | 不进行SSL证书验证 |
| verify='/path/to/certfile' | 指定受信任 CA 证书的 CA_BUNDLE 文件或目录;如果verify设置为目录的路径,则必须使用c_rehashOpenSSL 提供的实用程序处理该目录。 |
| REQUESTS_CA_BUNDLE环境变量指定CA证书 | |
| CURL_CA_BUNDLE指定CA证书 |
通过cert指定客户端证书、单个文件(包含私钥和证书)或两个文件路径的元组:
requests.get('https://kennethreitz.org', cert=('/path/client.cert', '/path/client.key'))
注意:本地证书的私钥必须未加密。目前,Requests 不支持使用加密密钥。
6.CA证书 7.Body Content Workflow默认情况下,当发出请求时,会立即下载响应正文;在请求时添加参数stream=True会推迟下载响应正文,直到访问Response.content时才下载;
8.Keep-Alive 9.Streaming UploadsRequests 支持流式上传,允许您发送大型流或文件而无需将它们读入内存。流式传输和上传,只需为您的 body 提供一个类似文件的对象:
with open('massive-body', 'rb') as f:
requests.post('http://some.url/streamed', data=f)
注意:强烈建议您以二进制模式打开文件。这是因为 Requests 可能会尝试Content-Length为您提供标头,如果这样做,此值将设置为文件中的字节数。如果以文本模式打开文件,可能会出现错误。
10. Chunk-Enabled Requests 11.POST Multiple Multipart-Encoded Files;一次上传多个文件url = 'https://httpbin.org/post'
multiple_files = [
('images', ('foo.png', open('foo.png', 'rb'), 'image/png')),
('images', ('bar.png', open('bar.png', 'rb'), 'image/png'))]
r = requests.post(url, files=multiple_files)
r.text
12.Event Hooks
import requests
url = 'https://httpbin.org/post'
# 设置钩子函数
def verify_res(res, *args, **kwargs):
print('url', res.url)
res.status='PASS' if res.status_code == 200 else 'FAIL'
res = requests.get(url, hooks={'response': verify_res})
print(res.text)
print(res.status)
13.Custom Authentication
14.Streaming Requests
15.Proxies
16.SOCKS
17.Compliance
18.Encodings
19.HTTP Verbs
20.Custom Verbs
21.Link Headers
22.Transport Adapters
23.Example: Specific SSL Version
24.Blocking Or Non-Blocking?
25.Header Ordering
26.Timeouts


