Python HTTPSConnection.close() 似乎没有关闭连接?
我不确定这是一个错误还是我只是做错了什么。如果我要像这样进行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,如果您发送“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.