2012-03-29 127 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

相關問題