Python BaseHttpServer, wie fange ich / flasche "gebrochene Pipe" -Fehler?
-
16-11-2019 - |
Frage
Ich baue eine kurze URL-Übersetzer-Engine in Python, und ich sehe eine Tonne "gebrochene Pfeife" -Fehler, und ich bin neugierig, wie man es am besten fangen soll, wenn Sie die BaseHttpserver-Klassen am besten fangen.Dies ist nicht der gesamte Code, sondern gibt Ihnen eine Vorstellung davon, was ich bisher tue: generasacodicetagpre.
Der Code selbst funktioniert großartig, begann jedoch Fehlern fast sofort, wenn in der Produktion: generasacodicetagpre.
Der Großteil dieser Fehler scheint davon zu stammen, ein Problem aufzurufen, das Send_Header () -Methode aufzurufen, wo alles, wo alles, was ich schreibe, ist dies: generasacodicetagpre.
Ich bin also neugierig, wo ich in meinem Code versuche, für diese IO-Ausnahme zu fangen. Ich schreibe probieren / außer aufrufen um jedes der self.send_header / self.end_header / self.wfile.write-Anrufe?Der andere Fehler, den ich von Zeit zu Zeit sehe, ist dies, aber nicht sicher, welche Ausnahme, um dies sogar zu fangen: generasacodicetagpre.
Lösung
The "broken pipe" exception means that your code tried to write to a socket/pipe which the other end has closed. If the other end is a web browser, the user could have stopped the request. You can ignore the traceback; it does not indicate a serious problem. If you want to suppress the message, you can put a try ... except block around all of the code in your http_output function, and log the exception if you like.
Additionally, if you want your HTTP server to process more than one request at a time, you need your server class to use one of the SocketServer.ForkingMixIn and SocketServer.ThreadingMixIn classes. Check the documentation of the SocketServer module for details.
Add: The "connection reset by peer" exception means that your code tried to read from a dead socket. If you want to suppress the traceback, you will need to extend the BaseHTTPServer class and override the handle_one_request method to add a try ... except block. You will need a new server class anyway, to implement the earlier suggestion about processing more than one request at a time.
Andere Tipps
This appears to be a bug in SocketServer, see this link Python Bug: 14574
A fix (works for me in Python 2.7) is to override the SocketServer.StreamRequestHandler finish() method, something like this:
...
def finish(self,*args,**kw):
try:
if not self.wfile.closed:
self.wfile.flush()
self.wfile.close()
except socket.error:
pass
self.rfile.close()
#Don't call the base class finish() method as it does the above
#return SocketServer.StreamRequestHandler.finish(self)
In my application, the error didn't occur in finish()
, it occurred in handle()
. This fix catches the broken pipe
errors:
class MyHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
...
def handle(self):
try:
BaseHTTPServer.BaseHTTPRequestHandler.handle(self)
except socket.error:
pass