使用 keep-alive 可以提高 Python 请求的速度

在 HTTP 协议中,您可以使用 keep-alive 在一个套接字中发送多个请求,然后立即接收来自服务器的响应,这将大大加快整个过程。在 python 请求库中有什么方法可以做到这一点吗?或者有没有其他方法可以很好地使用 request lib 来加速这个过程?

87091 次浏览

Yes, there is. Use requests.Session and it will do keep-alive by default.

I guess I should include a quick example:

import logging
import requests


logging.basicConfig(level=logging.DEBUG)
s = requests.Session()
s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
s.get('http://httpbin.org/cookies/set/anothercookie/123456789')
r = s.get("http://httpbin.org/cookies")
print(r.text)

You will note that these log message occur

INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): httpbin.org
DEBUG:requests.packages.urllib3.connectionpool:"GET /cookies/set/sessioncookie/123456789 HTTP/1.1" 302 223
DEBUG:requests.packages.urllib3.connectionpool:"GET /cookies HTTP/1.1" 200 55
DEBUG:requests.packages.urllib3.connectionpool:"GET /cookies/set/anothercookie/123456789 HTTP/1.1" 302 223
DEBUG:requests.packages.urllib3.connectionpool:"GET /cookies HTTP/1.1" 200 90
DEBUG:requests.packages.urllib3.connectionpool:"GET /cookies HTTP/1.1" 200 90

If you wait a little while, and repeat the last get call

INFO:requests.packages.urllib3.connectionpool:Resetting dropped connection: httpbin.org
DEBUG:requests.packages.urllib3.connectionpool:"GET /cookies HTTP/1.1" 200 90

Note that it resets the dropped connection, i.e. reestablishing the connection to the server to make the new request.