Python请求使用keep-alive加速 [英] Python requests speed up using keep-alive

查看:168
本文介绍了Python请求使用keep-alive加速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在HTTP协议中,您可以使用keep-alive在一个套接字中发送许多请求,然后立即从服务器接收响应,这样可以显着加快整个过程。在python请求lib中有没有办法做到这一点?或者有没有其他方法可以使用请求lib加快这一点?

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

如果你等一会儿,重复上一次获取调用

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆