Python http.client.HTTPConnection的close()函数不起作用?
我使用的Python版本是3.3。 假设我无法发送http请求,连接已关闭。但实际上,在调用close函数后,它仍然有效。这是我的密码:Python http.client.HTTPConnection的close()函数不起作用?,python,http,python-3.x,websocket,Python,Http,Python 3.x,Websocket,我使用的Python版本是3.3。 假设我无法发送http请求,连接已关闭。但实际上,在调用close函数后,它仍然有效。这是我的密码: conn = http.client.HTTPConnection("192.168.8.186:8081") conn.request("GET", "") response = conn.getresponse() data = response.read() data b"403 Forbidden \nRequest forbidd
conn = http.client.HTTPConnection("192.168.8.186:8081")
conn.request("GET", "")
response = conn.getresponse()
data = response.read()
data
b"403 Forbidden \nRequest forbidden by administrative rules.\n\n'"
conn.close() #???
conn.request("GET", "")
(conn.getresponse()).read()
b'403 Forbidden\nRequest forbidden by administrative rules.\n\n'
从HTTPConnection的代码来看,请求调用似乎依次调用send,如果未打开连接,send将打开连接:
def send(self, data):
"""Send `data' to the server."""
if self.sock is None:
if self.auto_open:
self.connect()
else:
raise NotConnected()
...
从这段代码来看,将auto_open标志默认设置为0即1可以避免这种行为。在这种情况下,您可能需要自己调用connect函数
但是,这是基于我的Python 2.7.3代码。从HTTPConnection的代码来看,请求调用似乎依次调用send,如果没有打开连接,它将打开连接:
def send(self, data):
"""Send `data' to the server."""
if self.sock is None:
if self.auto_open:
self.connect()
else:
raise NotConnected()
...
从这段代码来看,将auto_open标志默认设置为0即1可以避免这种行为。在这种情况下,您可能需要自己调用connect函数
但是,这是基于我的Python 2.7.3代码。它是。它是。