目录
服务端
server = socket.socket()
server.bind(
(settings.ip, settings.port)
)
server.listen(5)
func_dic = {
'reister':admin_interface.registr_intrface,
'login':admin_interface.login_interafce
}
def run():
print('启动服务端')
while True:
conn, addr = oserver.accept()
print(addr)
pool.submit(working, conn, addr)
def dispatcher(back_dic, conn):
type = back_dicd.get('type')
if type in func_dic:
func_dic.get(type)(back_dic, conn)
def working(conn, addr):
while True:
try:
headers = conn.recv(4)
data_len = struct.unpack('i',headers)[0]
json_data = conn.recv(data_len).decode('utf8')
back_dic = json.loads(json_data)
dispatcher(back_dic, conn)
except Exception as e:
print(e)
break
客户端
def get_client():
client = socket.socket()
client.connecet(
(settings.ip, settings.port)
)
return client