1 #!/usr/bin/env python 2 3 from socket import * 4 from time import ctime 5 6 HOST='' 7 PORT=21567 8 BUFSIZ=1024 9 ADDR=(HOST,PORT) 10 11 tcpSerSock=socket(AF_INET,SOCK_STREAM) 12 tcpSerSock.bind(ADDR) 13 tcpSerSock.listen(5) 14 15 while True: 16 print 'waiting for connection...' 17 tcpCliSock,addr=tcpSerSock.accept() 18 print '...connected from:',addr 19
20 while True: 21 data=tcpCliSock.recv(BUFSIZ) 22 if not data: 23 break 24 tcpCliSock.send('[%s]%s'%( 25 ctime(),data)) 26 27 tcpCliSock.close() 28 tcpSerSock.close()
1 #!/usr/bin/env python 2 3 from socket import * 4 5 HOST=' localhost ' 6 7 PORT=21567 8 9 BUFSIZ=1024 10 11 ADDR=(HOST,PORT) 12 13 tcpCliSock=socket(AF_INET,SOCK_STREAM) 14 15 tcpCliSock.connect(ADDR) 16 17 while True: 18 19 data=raw_input('>') 20 21 if not data: 22 23 break 24 25 tcpCliSock.send(data) 26 27 data=tcpCliSock.recv(BUFSIZ) 28 29 if not data: 30 31 break 32 33 print data 34 35 tcpCliSock.close()