2012-03-29 129 views

回答

1

您可以将这些方法添加到您的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,你可以添加一些代码,您希望在每次发生套接字错误(例如,客户端关闭连接)时执行。

+0

谢谢你的建议。 – 2012-03-30 07:58:26

+0

很高兴我能帮忙!欢迎来到Stackoverflow! :) – 2012-03-30 10:24:10

+0

只有在向客户端发送数据时才会抛出socket.error(self.wfile.write)。你知道有什么方法可以确定管道是否损坏而不尝试发送任何东西? – 2012-09-19 03:41:21

相关问题