且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python-Requests关闭http连接

更新时间:2021-09-07 04:08:14

如上所述这里,确实没有像HTTP连接这样的事情以及httplib所指的HTTPConnection实际上是底层的TCP连接,它根本不了解你的请求。请求抽象离开,你将永远不会看到它。

As discussed here, there really isn't such a thing as an HTTP connection and what httplib refers to as the HTTPConnection is really the underlying TCP connection which doesn't really know much about your requests at all. Requests abstracts that away and you won't ever see it.

最新版本的请求实际上确保在你的请求后保持TCP连接活着..如果你想要要关闭的TCP连接,你可以配置不使用的请求保持活着

The newest version of Requests does in fact keep the TCP connection alive after your request.. If you do want your TCP connections to close, you can just configure the requests to not use keep-alive.

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