python,利用socketserver模块实现并发聊天
利用socketserver模块很容易实现并发功能,下面的server.py和client.py程序实现了这一功能.
#server.pyimport socketserver class MyServer(socketserver.BaseRequestHandler): def handle(self): while True: conn = self.request while True: data = conn.recv(1024) if str(data,‘utf8‘) == ‘q‘: break print(str(data,‘utf8‘)) inp = input(‘>>>‘) conn.send(bytes(inp,‘utf8‘)) conn.close() if __name__ == ‘__main__‘: server = socketserver.ThreadingTCPServer((‘127.0.0.1‘,8000),MyServer) server.serve_forever()
#client.py import socket sk = socket.socket() address = (‘127.0.0.1‘,8000) sk.connect(address) while True: inp = input(‘>>>‘) if inp == ‘q‘: print(‘Bye-bye‘) break sk.send(bytes(inp,‘utf8‘)) data = sk.recv(1024) print(str(data,‘utf8‘))
相关推荐
wwwsurfphpseocom 2020-10-28
learningever 2020-09-19
周游列国之仕子 2020-09-21
voiletbin 2020-07-26
ldcwang 2020-07-26
mjshldcsd 2020-06-14
88236637 2020-06-03
81214051 2020-06-01
阳历六月 2020-05-14
bluet00 2020-05-12
wintershii 2020-04-21
luckyxl0 2020-04-19
MissFuTT 2020-04-15
88384957 2020-03-27
airfling 2020-02-22