Python HTTPSConnection.close() 似乎没有关闭连接?

发布于 2024-08-27 21:19:42 字数 1049 浏览 2 评论 0原文

我不确定这是一个错误还是我只是做错了什么。如果我要像这样进行 HTTP 连接:

import httplib

http_connection = httplib.HTTPConnection("192.168.192.196")
http_connection.request("GET", "/")
http_connection.sock.settimeout(20)
response = http_connection.getresponse()
data = response.read()
http_connection.close()

然后在 DOS 提示符下,我会这样做:

netstat -ano | find /i "192.168.192.196:80" | find /i "ESTABLISHED"

我什么也得不到。

但是,如果我做同样的事情,但将其更改为 HTTPSConnection:

import httplib

http_connection = httplib.HTTPSConnection("192.168.192.196")
http_connection.request("GET", "/")
http_connection.sock.settimeout(20)
response = http_connection.getresponse()
data = response.read()
http_connection.close()

然后执行以下操作:

netstat -ano | find /i "192.168.192.196:443" | find /i "ESTABLISHED"

我实际上会看到连接保持建立状态,直到我实际 ^Z 退出 Python shell。

这发生在我负责的一个应用程序中。 Python 实际上并没有挂在那里——它只是让连接保持打开状态。

我在这里做错了什么吗?我需要额外的代码来关闭 HTTPS 连接吗?

顺便说一句,这是 Python 2.6.4。

I'm not sure if this is a bug or if I'm just doing something wrong. If I were to do an HTTP connection like this:

import httplib

http_connection = httplib.HTTPConnection("192.168.192.196")
http_connection.request("GET", "/")
http_connection.sock.settimeout(20)
response = http_connection.getresponse()
data = response.read()
http_connection.close()

Then at a DOS prompt, I do this:

netstat -ano | find /i "192.168.192.196:80" | find /i "ESTABLISHED"

I get nothing.

However, if I do the same thing, but change it to an HTTPSConnection:

import httplib

http_connection = httplib.HTTPSConnection("192.168.192.196")
http_connection.request("GET", "/")
http_connection.sock.settimeout(20)
response = http_connection.getresponse()
data = response.read()
http_connection.close()

Then do this:

netstat -ano | find /i "192.168.192.196:443" | find /i "ESTABLISHED"

I will actually see that the connection remains established until I actually ^Z out of the Python shell.

This is happening in one of the applications I'm responsible for. Python isn't actually hanging there - it's simply leaving the connection open.

Am I doing something wrong here? Do I need extra code to close the HTTPS connection?

This is Python 2.6.4, btw.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦过后 2024-09-03 21:19:42

事实证明,如果您发送“Connection:close”HTTP 标头,这不是问题 - 尽管我仍然认为 .close() 实际上应该像 HTTPConnection 那样关闭连接。

Turns out that if you send the "Connection: close" HTTP header, this isn't an issue - although I still think that .close() should actually close the connection like it does for HTTPConnection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文