0

I'm writing an asyncore server which fetches info from another module in same process and writes it back to client. The info basically is a dictionary where for each key we have a queue of messages. I'm required to dump the length of each queue. The code runs fine on a test machine but as soon as I install it on a production server I start getting following error message : "socket.error'>:[Errno 32] Broken pipe)".

This is the server:

class request_handler (asyncore.dispatcher):
    def __init__(self, conn_sock, client_address, dict):
            self.client_address  = client_address
            self.buffer = ""
            self.dict = dict
            asyncore.dispatcher.__init__(self, conn_sock)

    def readable(self):
            return True

    def writable(self):
            return False

    def handle_read(self):
            data = self.recv(SIZE)
            mtats = "msgq-stats"

            if data:
                    buffer = data
                    if buffer.lower() == mstats.lower():
                            msgout = "-- Message Queue Stats --\n"
                            for key, value in dict.items():
                                    mq = 0
                                    if dict[key].message_queue:
                                            mq = len(dict[key].message_queue)
                                    msgout += key + ":" + str(mq) + "\n"
                            self.send(msgout)
                    else:   self.send("Invalid input\n")
            else:
                    self.send("Invalid input\n")

    def handle_write(self):
            print ("--Handling read--\n")

    def handle_close(self):
            pass

# ---------------------------------------------------------------------

class monitor_server (asyncore.dispatcher):
    def __init__ (self, ip, port, destination):
            sys.path.append('/path/')
            import dict

            self.ip = ip
            self.port = port
            self.dict = dict
            asyncore.dispatcher.__init__ (self)
            self.create_socket (socket.AF_INET, socket.SOCK_STREAM)

            self.set_reuse_addr()
            self.bind ((ip, port))
            self.listen (5)

    def writable (self):
            return 0

    def handle_read (self):
            pass

    def readable (self):
            return self.accepting

    def handle_connect (self):
            pass

    def handle_accept (self):
             (conn_sock, client_address) = self.accept()
             request_handler (conn_sock, client_address, self.destination)

and this is the client code:

class Client(asyncore.dispatcher_with_send):
   def __init__(self, host, port, message):
      asyncore.dispatcher.__init__(self)
      self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
      self.connect((host, port))
      print "Message being sent is "
      print message
      self.out_buffer = message

  def handle_close(self):
      self.close()

  def handle_read(self):
      print self.recv(1024)
      self.close()

 c = Client('', 6000, 'msgq-stats')
 asyncore.loop()

Thanks in advance.

4

1 回答 1

0

这是您必须处理的错误情况。有时连接会关闭。与生产相比,在开发过程中遇到不同的套接字错误是正常的,因为有很多不同的可能错误,它们几乎完全取决于执行环境以及连接另一端的程序正在做什么以及什么客户端和服务器之间的所有路由器都决定这样做。

所以,你的问题的字面答案是你需要在你的应用程序代码中处理这个和许多其他套接字错误。当您使用 asyncore 时,这是您工作的一部分。添加必要的异常处理并在发生此类情况时将连接标记为已关闭。

一个稍微好一点的答案是,有更高级的工具可以使网络编程更容易,您可能应该考虑使用这些工具。这个领域的大事是Twisted

于 2013-02-07T00:13:52.170 回答