1 #!usr/bin/python 2 3 from socket import * 4 from select import * 5 from time import ctime 6 s=socket() 7 s.bind(('127.0.0.1',6653)) 8 9 d={s.fileno():s} 10 s.listen(5) 11 12 p=poll() 13 p.register(s) 14 15 while 1: 16 events=p.poll() 17 for fd,event in events: 18 if fd==s.fileno(): 19 c,addr=s.accept() 20 print "connect from ",addr 21 p.register(c) 22 d[c.fileno()]=c 23 elif event & POLLIN: 24 data=d[fd].recv(1024) 25 if not data: 26 print d[fd].getpeername(),"discponnect" 27 p.unregister(fd) 28 del d[fd] 29 else: 30 print data 31 d[fd].send("[%s]%s"%(ctime(),data)) 32 s.close()