标签:bre ever tin __name__ def enc 客户 exce client
服务器端sockforever版
import socketserver class MyServer(socketserver.BaseRequestHandler):
def handle(self): # print(‘conn is: ‘,self.request) print(‘address is:‘,self.client_address) while True: try: msg = self.request.recv(1024) if not msg:break print(‘收到客户端的消息: ‘,msg.decode(‘utf-8‘),self.client_address) self.request.send(msg) except Exception as e: print(e) break if __name__ == ‘__main__‘: server = socketserver.ThreadingTCPServer((‘127.0.0.1‘,8080),MyServer) server.serve_forever()
client端
import socket buffer_size=1024 sk = socket.socket() sk.connect((‘127.0.0.1‘,8080)) while True: msg = input(‘>>:‘) if not msg:continue if msg == ‘quit‘:break sk.send(msg.encode(‘utf-8‘)) data =sk.recv(buffer_size) print(‘来自客户端的消息: ‘,data.decode(‘utf-8‘)) sk.close()
标签:bre ever tin __name__ def enc 客户 exce client
原文地址:https://www.cnblogs.com/kuraki/p/9575841.html