java一些小的技术分享(java新技术分享)
267
2022-08-25
python-- socketServer(python培训)
socketServer
客户端
import socketclient = socket.socket()client.connect(('localhost', 9999))while True: msg = input('>>>>:').strip() if len(msg) == 0: continue client.send(msg.encode('utf-8')) data = client.recv(1024) print("recv:", data.decode())client.close()
服务端
import socketserverclass MyTcpHandler(socketserver.BaseRequestHandler): def handle(self): while True: try: self.data = self.request.recv(1024).strip() print("{} wrote:".format(self.client_address[0])) print(self.data) self.request.send(self.data.upper()) except ConnectionResetError as e: print('err', e) breakif __name__ == "__main__": HOST, PORT = 'localhost', 9999 server = socketserver.TCPServer((HOST, PORT), MyTcpHandler) server.serve_forever()
socketServer 多并发
客户端
import socketclient = socket.socket()client.connect(('localhost', 9999))while True: msg = input('>>>>:').strip() if len(msg) == 0: continue client.send(msg.encode('utf-8')) data = client.recv(1024) print("recv:", data.decode())client.close()
服务端
import socketserverclass MyTcpHandler(socketserver.BaseRequestHandler): def handle(self): while True: try: self.data = self.request.recv(1024).strip() print("{} wrote:".format(self.client_address[0])) print(self.data) self.request.send(self.data.upper()) except ConnectionResetError as e: print('err', e) breakif __name__ == "__main__": HOST, PORT = 'localhost', 9999 server = socketserver.ThreadingTCPServer((HOST, PORT), MyTcpHandler) server.serve_forever()
可以同时处理多个客户端
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~