Python 请求使用 keep-alive 加速 [英] Python requests speed up using keep-alive
问题描述
在 HTTP 协议中,您可以使用 keep-alive 在一个套接字中发送多个请求,然后立即从服务器接收响应,这将显着加快整个过程.有没有办法在python请求库中做到这一点?或者有没有其他方法可以使用请求库来加快速度?
In the HTTP protocol you can send many requests in one socket using keep-alive and then receive the response from server at once, so that will significantly speed up whole process. Is there any way to do this in python requests lib? Or are there any other ways to speed this up that well using requests lib?
推荐答案
是的,有.使用 requests.Session
和 默认会保持活动状态.
我想我应该包括一个简单的例子:
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
如果你稍等片刻,并重复最后一次get
调用
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.
这篇关于Python 请求使用 keep-alive 加速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!