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