Python-Requests关闭http连接 [英] Python-Requests close http connection

查看:10635
本文介绍了Python-Requests关闭http连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,你如何关闭与Requests的连接(python-requests.org)?

I was wondering, how do you close a connection with Requests (python-requests.org)?

使用 httplib 它是 HTTPConnection.close(),但我如何对请求做同样的事情?

With httplib it's HTTPConnection.close(), but how do I do the same with Requests?

代码如下:

    r = requests.post("https://stream.twitter.com/1/statuses/filter.json", data={'track':toTrack}, auth=('username', 'passwd'))

    for line in r.iter_lines():
        if line:
            self.mongo['db'].tweets.insert(json.loads(line))

提前致谢。

推荐答案

如上所述< a href =https://stackoverflow.com/questions/3272653/in-what-c​​onditions-are-closing-a-http-connection-necessary/3272700#3272700>这里,确实没有像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

这篇关于Python-Requests关闭http连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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