2
我在写http服务器,可以将大文件提供给客户端。如何知道使用BaseHTTPRequestHandler客户端关闭连接
在写入wfile流时,客户端可能会关闭连接,并且我的服务器可能会发生套接字错误(Errno 10053)。
客户端关闭连接时可以停止写入吗?
我在写http服务器,可以将大文件提供给客户端。如何知道使用BaseHTTPRequestHandler客户端关闭连接
在写入wfile流时,客户端可能会关闭连接,并且我的服务器可能会发生套接字错误(Errno 10053)。
客户端关闭连接时可以停止写入吗?
您可以将这些方法添加到您的BaseHTTPRequestHandler类,这样就可以知道,如果客户端关闭了连接:
def handle(self):
"""Handles a request ignoring dropped connections."""
try:
return BaseHTTPRequestHandler.handle(self)
except (socket.error, socket.timeout) as e:
self.connection_dropped(e)
def connection_dropped(self, error, environ=None):
"""Called if the connection was closed by the client. By default
nothing happens.
"""
# add here the code you want to be executed if a connection
# was closed by the client
在第二种方法:connection_dropped,你可以添加一些代码,您希望在每次发生套接字错误(例如,客户端关闭连接)时执行。
谢谢你的建议。 – 2012-03-30 07:58:26
很高兴我能帮忙!欢迎来到Stackoverflow! :) – 2012-03-30 10:24:10
只有在向客户端发送数据时才会抛出socket.error(self.wfile.write)。你知道有什么方法可以确定管道是否损坏而不尝试发送任何东西? – 2012-09-19 03:41:21