####client#### from socket import * HOST='localhost' PORT=21567 BUFSIZ=1024 ADDR=(HOST,PORT) while True: tcpCliSock=socket(AF_INET,SOCK_STREAM) tcpCliSock.connect(ADDR) data=raw_input('input your words:') if not data: break tcpCliSock.send('%s\r\n' %data) data='......['+str(tcpCliSock.getsockname())+tcpCliSock.recv(BUFSIZ) if not data: break print data.strip() print tcpCliSock.close() ####server#### from SocketServer import (TCPServer as TCP,StreamRequestHandler as SRH) from time import ctime HOST='' PORT=21567 ADDR=(HOST,PORT) class MyRequestHandler(SRH): def handle(self): #print '...connect form...',self.client_address print '......[%s%s]:%s' %(self.client_address,ctime(),self.rfile.readline()) inp=raw_input('input you words:') self.wfile.write('%s:]%s' %(ctime(),inp)) tcpServ=TCP(ADDR,MyRequestHandler) print 'waitint for connection...' tcpServ.serve_forever()本文章来至源码世界 http://www.ymsky.net/views/65543.shtml
socket单工聊天工具
最新推荐文章于 2022-08-02 14:19:56 发布