• Python code demo - [Python Network Socket ]2005-12-08

    Python code demo

    
    
    def run( self ):
    
      while 1:
    
        # Await an event on a readable socket descriptor
        (sread, swrite, sexc) = select.select( self.descriptors, [], [] )
    
        # Iterate through the tagged read descriptors
        for sock in sread:
    
          # Received a connect to the server (listening) socket
          if sock == self.srvsock:
            self.accept_new_connection()
          else:
    
            # Received something on a client socket
            str = sock.recv(100)
    
            # Check to see if the peer socket closed
            if str == '':
              host,port = sock.getpeername()
              str = 'Client left %s:%s\r\n' % (host, port)
              self.broadcast_string( str, sock )
              sock.close
              self.descriptors.remove(sock)
            else:
              host,port = sock.getpeername()
              newstr = '[%s:%s] %s' % (host, port, str)
              self.broadcast_string( newstr, sock )
    


    Tags: Python Network Socket

    发表于 12:47:10 | 引用 0 | 编辑

评论

  • 出自 IBM developWorks

     回复 d 说:
    哦,是的啊。
    (2005-12-08 16:02:26)

    d () 发表于 2005-12-08 15:19:42   [回复]

发表评论