import socketserver
class MyServer(socketserver.BaseRequestHandler):
def handle(self): #继承BaseRequestHandler后就要重写handle方法
print(self.request) #相当于coon
print(self.client_address) #相当于addr
while True:
try:
#收消息
data = self.request.recv(1024)
if not data:break
print('检测到来自客户端的链接',self.client_address,data)
#发送消息
self.request.sendall(data.upper())
except Exception as e:
print('检测到异常的形式是',e)
break
if __name__ == '__main__':
# 多线程服务端,可以实现并发,可以同时开启多个任务等待链接,ThreadingTCPServer(tuple格式的(ip+prot),类名)
s = socketserver.ThreadingTCPServer(('192.168.55.1',8000),MyServer)
#永久服务,链接循环
s.serve_forever()
转载于:https://www.cnblogs.com/pandaboy1123/p/8544596.html