Document content as used hereinafter
"userinfo.txt"
zhangsan: e7a6d0f97db7ea1c4a0c1c137cbf771c lisi: c79439cf9abcbd6c6d46e45766a9e64a wangwu: f77599e7005939d280742dc8eb5db89a zhaoliu: 00ce36c50bbc8c067a5a7a628ca14688
Server
# ### server Import hashlib Import socket Import json DEF get_md5_code (usr, pwd): # username salt thereof as an encryption Key HM = hashlib.md5 (usr.encode ()) hm.update(pwd.encode()) res = hm.hexdigest() return res sk = socket.socket, (), sk.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) sk.bind( ("127.0.0.1",9000) ) sk.listen() conn,addr = sk.accept() # Processing data transceiver logic # to accept a binary byte stream -> String MSG = conn.recv (1024 ) .decode () Print (MSG, type (MSG)) # string -> dictionary DIC = json.loads (msg) Print (dic, of the type (dic)) # Identifier, default False Sign = False with open("userinfo.txt",mode="r",encoding="utf-8") as fp: for line in fp: usr, pwd = line.strip () Split (. " : " ) IF usr == dic [ " username " ] and pwd == get_md5_code (dic [ " username " ], dic [ " password " ]): # successful landing return it to the state. 1 RES = { " code " :. 1 } res_msg = json.dumps(res).encode() conn.send(res_msg) # If the login is successful, change the default True Sign = True BREAK IF Sign == False: # login fails, return a status code of 0 RES = { " code " : 0} conn.send(json.dumps(res).encode()) # Fourth wave conn.Close () # refunded port sk.close () """ # Review: file object is an iterator fp = open("userinfo.txt",mode="r",encoding="utf-8") from collections import Iterator,Iterable print( isinstance(fp,Iterator) ) """
Client
# ### client Import socket, json client = socket.socket() client.connect( ("127.0.0.1",9000) ) usr = the INPUT ( " Please enter your account: " ) pwd = the INPUT ( " Please enter your password: " ) DIC = { " username " : usr, " password " : pwd, " Status " : " Login " } # Returns formatted string json RES = json.dumps (DIC) # in converting json string into a byte stream bytes_msg = res.encode () # is sent to the server client.send (bytes_msg) # Receiving data sent by the server back res_msg = client.recv (1024 ) .decode () # string into the dictionary dic_code = json.loads (res_msg) IF dic_code [ " code " ]: Print ( " Congratulations, successful landing " ) the else : Print ( " Sorry, login failed " ) # Close links client.close ()