سؤال

أقوم ببناء محرك مترجم URL قصير في بيثون، وأرى طن من أخطاء "الأنابيب المكسورة"، وأنا فضولي كيفية فخ الأمر بشكل أفضل عند استخدام فصول BaseHtTPServer.هذا ليس الكود بالكامل، لكنه يمنحك فكرة عما أفعله حتى الآن: giveacodicetagpre.

الرمز نفسه يعمل بشكل رائع، ولكنه بدأ رمي الأخطاء على الفور تقريبا عند الإنتاج: giveacodicetagpre.

الجزء الأكبر من هذه الأخطاء يبدو أنه ينبع من وجود مشكلة في الاتصال بطريقة send_header () حيث كل ما أكتبه هو: giveacodicetagpre.

لذلك أنا فضولي حيث في التعليمات البرمجية لمحاولة فخ هذا استثناء IO ... هل أكتب محاولة / ما عدا المكالمات حول كل من self.send_header / self.end_headers / self.wfile.wfile.الخطأ الآخر الذي أراه من وقت لآخر هو هذا واحد، ولكن ليس متأكدا من استثناء ما لمشاهدة حتى اللحاق بهذا: giveacodicetagpre.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top