網絡編程基礎

目錄python

服務端json

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

客戶端socket

def get_client():
    client = socket.socket()
    client.connecet(
        (settings.ip, settings.port)
    )
    return client
相關文章
相關標籤/搜索