@@ -0,0 +1,2114 @@
+# Copyright (c) 2014 ASMlover. All rights reserved.%0A#%0A# Redistribution and use in source and binary forms, with or without%0A# modification, are permitted provided that the following conditions%0A# are met:%0A#%0A#  * Redistributions of source code must retain the above copyright%0A#    notice, this list ofconditions and the following disclaimer.%0A#%0A#  * Redistributions in binary form must reproduce the above copyright%0A#    notice, this list of conditions and the following disclaimer in%0A#    the documentation and/or other materialsprovided with the%0A#    distribution.%0A#%0A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS%0A# %22AS IS%22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT%0A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS%0A# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE%0A# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,%0A# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,%0A# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;%0A# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER%0A# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT%0A# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN%0A# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE%0A# POSSIBILITY OF SUCH DAMAGE.%0A#%0A#!/usr/bin/env python%0A# -*- encoding: utf-8 -*-%0A%0Aimport socket%0Aimport select%0A%0A%0Adef start_server():%0A  s = socket.socket()%0A  host = socket.gethostname()%0A  port = 1234%0A  s.bind((host, port))%0A%0A  fdmap = %7Bs.fileno(): s%7D%0A%0A  s.listen(5)%0A  poll = select.poll()%0A  poll.register(s)%0A  while True:%0A    events = poll.poll()%0A    for fd, ev in events:%0A      if fd in fdmap:%0A        c, addr = s.accept()%0A        print 'Got connection from', addr %0A        poll.register(c)%0A        fdmap%5Bc.fileno()%5D = c%0A      elif ev & select.POLLIN:%0A        data = fdmap%5Bfd%5D.recv(1024)%0A        if not data:%0A          print fdmap%5Bfd%5D.getpeername(), 'disconnected'%0A          poll.unregister(fd)%0A          del fdmap%5Bfd%5D%0A        else:%0A          print data %0A%0A%0A%0Aif __name__ == '__main__':%0A  start_server()%0A

add demo about using poll