socketserver模块
server
固定写法
import socketserver
class MYserver(socketserver.BaseRequestHandler):
def handle(self):
"""
所有业务逻辑都在这
:return:
"""
while 1:
from_client_data=self.request.recv(1024)
print(f'来自客户端消息{from_client_data}.decode("utf-8")')
to_client_msg=input('>>>>>>>>>').encode('utf-8')
self.request.send(to_client_msg)
if __name__ == '__main__':
server=socketserver.ThreadingTCPServer(('127.0.0.1',6666),MYserver)
server.serve_forever()
client
# 自己的
import socket
phone=socket.socket()
phone.connect(('127.0.0.1',6666))
while 1:
to_server_data=input('>>>>>>q退出')
if not to_server_data:
print('不可以输入空')
continue
phone.send(to_server_data.encode('utf-8'))#因为不可以让他发空 所以放在判断下
if to_server_data.upper()=='Q':
break
from_server_data=phone.recv(1024)
print(f'来自客户端消息{from_server_data.decode("utf-8")}')
phone.close()
socketserver 原理是什么