+ -
当前位置:首页 → 问答吧 → 一段錯誤訊息

一段錯誤訊息

时间:2010-12-27

来源:互联网

  1. # -*- coding: utf-8 -*-

  2. import socket
  3. import asyncore


  4. servicePort = 443


  5. serviceHostAddress = 'ptt.cc'
  6. serviceHostPort = 443

  7. class AgentClient(asyncore.dispatcher_with_send):

  8.     def __init__(self, otherSocket=None):
  9.         asyncore.dispatcher_with_send.__init__(self, otherSocket)


  10.         self.client = False

  11.         self.ready = False

  12.         self.other = None

  13.         if not otherSocket:
  14.             self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
  15.         else:
  16.             self.client = True

  17.     def handle_error(self):


  18.         print "Error"
  19.         self.handle_close()

  20.     def handle_read(self):

  21.         
  22.         if self.other and self.other.ready:
  23.             data = self.recv(4096)

  24.             if data:
  25.                 self.other.send(data)

  26.     def handle_connect(self):

  27.         
  28.         self.ready = True
  29.         self.other.ready = True

  30.     def handle_close(self):


  31.         if self.client:
  32.             print "Close connection from : " + self.socket.getpeername()[0]
  33.         
  34.         self.ready = False
  35.         self.close()
  36.         if self.other and self.other.ready:
  37.             self.other.handle_close()

  38. class AgentServer(asyncore.dispatcher):

  39.    
  40.     def __init__(self, port):
  41.         asyncore.dispatcher.__init__(self)

  42.         self.port = port
  43.         self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
  44.         self.bind(('', self.port))
  45.         self.listen(5)
  46.    
  47.     def handle_accept(self):

  48.         
  49.         clientSocket, address = self.accept()
  50.         print 'New client from : ' + address[0]
  51.         
  52.         agentClient = AgentClient(clientSocket)
  53.         agentHost = AgentClient()


  54.         try:
  55.             agentHost.connect((serviceHostAddress, serviceHostPort))
  56.         except socket.error, (socketError, errorMessage):
  57.             print 'Failed to connect %s : %s' % (serviceHostAddress, errorMessage)
  58.             agentClient.handle_close()
  59.             return

  60.         agentHost.other = agentClient
  61.         agentClient.other = agentHost

  62. def cleanSocketMapUp():

  63.    

  64.     # RuntimeError: dictionary changed size during iteration
  65.     for fileno in asyncore.socket_map.keys():
  66.         asyncore.socket_map[fileno].close()

  67. agentServer = AgentServer(servicePort)
  68. print "Agent is serving at %d and target to %s:%d ..." % (servicePort, serviceHostAddress, serviceHostPort)
  69. print "Press Ctrl + C to stop service."
  70.    
  71. try:
  72.     asyncore.loop(1)
  73. except KeyboardInterrupt:
  74.     print "Stop agent service."
  75. finally:
  76.     agentServer.close()
  77.     cleanSocketMapUp()
复制代码
我是在linux 用 nohup 運行上面的代碼 , 那我發現它運行一陣子會掛掉

下面是他出現錯誤,是nohup.out的紀錄請問下面這段訊息是什麼原因造成?
  1.   1 Agent is serving at 443 and target to ptt.cc:443 ...
  2.   2 Press Ctrl + C to stop service.
  3.   3 New client from : 114.35.133.52
  4.   4 New client from : 222.79.245.84
  5.   5 Error
  6.   6 Traceback (most recent call last):
  7.   7   File "ptt.py", line 106, in <module>
  8.   8     asyncore.loop(1)
  9.   9   File "/usr/lib/python2.6/asyncore.py", line 202, in loop
  10. 10     poll_fun(timeout, map)
  11. 11   File "/usr/lib/python2.6/asyncore.py", line 143, in poll
  12. 12     read(obj)
  13. 13   File "/usr/lib/python2.6/asyncore.py", line 80, in read
  14. 14     obj.handle_error()
  15. 15   File "ptt.py", line 34, in handle_error
  16. 16     self.handle_close()
  17. 17   File "ptt.py", line 55, in handle_close
  18. 18     print "Close connection from : " + self.socket.getpeername()[0]
  19. 19   File "<string>", line 1, in getpeername
  20. 20 socket.error: [Errno 107] Transport endpoint is not connected
复制代码

作者: shihyu   发布时间: 2010-12-27

感觉像是socket已经断开了,不需要close了。

作者: nmweizi   发布时间: 2010-12-27

热门下载

更多