简体   繁体   English

Python-Requests(> = 1. *):如何禁用keep-alive?

[英]Python-Requests (>= 1.*): How to disable keep-alive?

I'm trying to program a simple web-crawler using the Requests module, and I would like to know how to disable its -default- keep-alive feauture. 我正在尝试使用Requests模块编写一个简单的Web爬虫程序,我想知道如何禁用它的-default- keep-alive feauture。

I tried using: 我试过用:

s = requests.session()
s.config['keep_alive'] = False

However, I get an error stating that session object has no attribute 'config', I think it was changed with the new version, but i cannot seem to find how to do it in the official documentation. 但是,我得到一个错误,说明会话对象没有属性'config',我认为它已经改变了新版本,但我似乎无法在官方文档中找到如何做到这一点。

The truth is when I run the crawler on a specific website, it only gets five pages at most, and then keeps looping around infinitely, so I thought it has something to do with the keep-alive feature! 事实上,当我在特定网站上运行爬虫时,它最多只能获得五个页面,然后无限循环,所以我认为它与保持活动功能有关!

PS: is Requests a good module for a web-crawler? PS:是请求网络爬虫的好模块吗? is there something more adapted? 有更适合的东西吗?

Thank you ! 谢谢 !

This works 这有效

s = requests.session()
s.keep_alive = False

Answered in the comments of a similar question. 在类似问题的评论中回答。

I am not sure but can you try passing {"Connection": "close"} as HTTP headers when sending a GET request using requests. 我不确定,但是当您使用请求发送GET请求时,您是否可以尝试将{“Connection”:“close”}作为HTTP标头传递。 This will close the connection as soon a server returns a response. 这将在服务器返回响应后立即关闭连接。

>>> headers = {"Connection": "close"}
>>> r = requests.get('https://example.xcom', headers=headers)

As @praveen suggested it's expected from us to use HTTP/1.1 header Connection: close to notify the server that the connection should be closed after completion of the response. 正如@praveen所建议的那样,我们希望使用HTTP/1.1标头Connection: close来通知服务器在完成响应后应该关闭连接。

Here is how it's described in RFC 2616 : 以下是RFC 2616中描述的内容:

HTTP/1.1 defines the "close" connection option for the sender to signal that the connection will be closed after completion of the response. HTTP / 1.1定义了“关闭”连接选项,发送方在完成响应后发出连接将被关闭的信号。 For example, 例如,

 Connection: close 

in either the request or the response header fields indicates that the connection SHOULD NOT be considered `persistent' (section 8.1) after the current request/response is complete. 在请求或响应头字段中,表示在当前请求/响应完成后,连接不应被视为“持久”(第8.1节)。

HTTP/1.1 applications that do not support persistent connections MUST include the "close" connection option in every message. 不支持持久连接的HTTP / 1.1应用程序必须在每条消息中包含“关闭”连接选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM