可以實現從客戶端輸入賬號和密碼然后發送到服務器進行驗證,實現用戶登入校正操作。
服務器:
import socket import json server = socket.socket() server.bind(('192.168.101.5', 8001)) server.listen(5) while True: coon, addr = server.accept() while True: ret = coon.recv(1024) Info = json.loads(ret) """ 因為嘗試了很多次,輸入都是正確的,但是都是顯示登入失敗 就想看看是不是哪里錯,從這里打印看發現都是字符串 沒有類型錯誤,但是就是一直發登入失敗, """ # print(Info) # print(Info['name'],type(Info['name'])) # print(Info['password'],type(Info['password'])) flag = False with open('Info.txt', 'r') as f: for line in f: # lst = line.strip(':') 從這里打印后,發現從文件讀取時,會把換行符讀取進去 # print(lst) 所以要加上strip()函數 name, passward = line.strip().split(':') # print(name,type(name)) # print(passward,type(passward)) if name == Info['name'] and passward == Info['password']: flag = True break if flag: coon.send('登入成功'.encode('utf-8')) else: coon.send('用戶名或密碼錯誤!'.encode('utf-8')) server.close()
客戶端:
ort socket import json client = socket.socket() client.connect(('192.168.101.5', 8001)) while True: while True: name = input(">>>請輸入賬號:") password = input(">>>請輸入密碼:") Info = json.dumps({'name':name, 'password':password}).encode('utf-8') # print(Info) client.send(Info) ret = client.recv(1024) print(ret.decode('utf-8')) client.close()
經過一番排查錯誤,總算是勉強實現了預期的功能。以后繼續完善和添加新的功能!