client
import socket import json sk = socket.socket() sk.connect(('127.0.0.1',9000)) def auth(opt): usr = input('please input name').strip() pwd = input('please input password').strip() dic = {"user":usr,"pwd":pwd,"operate":opt} str_dic = json.dumps(dic) sk.send(str_dic.encode()) #接受服务端的数据 file_info = sk.recv(1024).decode() file_dic = json.dumps(file_info) return file_dic res=auth('register') print(res) sk.close()
server:
import socketserver import json import os import hashlib base_path = os.path.dirname(__file__) userinfo = os.path.join(base_path,"db","userinfo.txt") class Auth(): @staticmethod def md5(usr,pwd): md5_obj = hashlib.md5(usr.encode()) md5_obj.update(pwd.encode()) return md5_obj.hexdigest() @classmethod def register(cls,opt_dic): with open(userinfo,mode='r',encoding='utf-8') as fp: for line in fp: username = line.split(":")[0] if username == opt_dic["user"]: return {"result":False,"info":"username is used"} with open(userinfo,mode='a+',encoding='utf-8') as fp: fp.write("%s:%s\n" % (opt_dic["user"],cls.md5(opt_dic["user"],opt_dic["pwd"]))) return {"result":True,"info":"successed"} class ftpServe(socketserver.BaseRequestHandler): def handle(self): opt_dic = self.myrecv() print(opt_dic) #反射处理不同类的函数 if hasattr(Auth,opt_dic["operate"]): res = getattr(Auth,opt_dic["operate"])(opt_dic) #将返回的数据发送给客户端 self.mysend(res) #接受数据 def myrecv(self): info = self.request.recv(1024) opt_str = info.decode() opt_dic = json.loads(opt_str) return opt_dic #发送数据 def mysend(self,send_info): #将字典->字符串->字节流 send_info = json.dumps(send_info).encode() self.request.send(send_info) myserver = socketserver.ThreadingTCPServer(("127.0.0.1",9000), ftpServe) #让一个端口绑定多个程序,一般用于测试。 socketserver.TCPServer.allow_reuse_address = True myserver.serve_forever()
改进版:
client
''' 待解决的问题:反射,静态应用与类的应用 ''' import socket import json sk = socket.socket() sk.connect(('127.0.0.1',9000)) def auth(opt): usr = input('please input name').strip() pwd = input('please input password').strip() dic = {"user":usr,"pwd":pwd,"operate":opt} str_dic = json.dumps(dic) sk.send(str_dic.encode()) #接受服务端的数据 file_info = sk.recv(1024).decode() file_dic = json.dumps(file_info) return file_dic def register(): res = auth('register') return res def login(): res = auth('login') return res def myexit(): opt_dic = {"operate":"myexit"} sk.send(json.dumps(opt_dic).encode()) exit("welcome come next") operate_list=[("login",login),("register",register),("myexit",myexit)] def main(): for i,tup in enumerate(operate_list,start=1): print(i,tup[0]) num = int(input("input num >>>>>").strip()) res = operate_list[num-1][1]() return res while True: res = main() print(res) sk.close()
server:
import socketserver import json import os import hashlib base_path = os.path.dirname(__file__) userinfo = os.path.join(base_path,"db","userinfo.txt") class Auth(): @staticmethod def md5(usr,pwd): md5_obj = hashlib.md5(usr.encode()) md5_obj.update(pwd.encode()) return md5_obj.hexdigest() @classmethod def register(cls,opt_dic): with open(userinfo,mode='r',encoding='utf-8') as fp: for line in fp: username = line.split(":")[0] if username == opt_dic["user"]: return {"result":False,"info":"username is used"} with open(userinfo,mode='a+',encoding='utf-8') as fp: fp.write("%s:%s\n" % (opt_dic["user"],cls.md5(opt_dic["user"],opt_dic["pwd"]))) return {"result":True,"info":"successed"} @classmethod def login(cls,opt_dic): with open(userinfo,mode='r',encoding='utf-8') as fp: for line in fp: username,password = line.strip().split(":") if username == opt_dic['user'] and password == cls.md5(opt_dic["user"],opt_dic["pwd"]): return {"result":True,"info":"login successed"} return {"resule":False,"info":'login fail'} @classmethod def myexit(cls,opt_dic): return {"result":"myexit"} class ftpServe(socketserver.BaseRequestHandler): def handle(self): while True: opt_dic = self.myrecv() print(opt_dic) #反射处理不同类的函数 if hasattr(Auth,opt_dic["operate"]): res = getattr(Auth,opt_dic["operate"])(opt_dic) if res['result']=="myexit": return #将返回的数据发送给客户端 self.mysend(res) else: dic = {"resule":False,"info":'no'} self.mysend(dic) #接受数据 def myrecv(self): info = self.request.recv(1024) opt_str = info.decode() opt_dic = json.loads(opt_str) return opt_dic #发送数据 def mysend(self,send_info): #将字典->字符串->字节流 send_info = json.dumps(send_info).encode() self.request.send(send_info) myserver = socketserver.ThreadingTCPServer(("127.0.0.1",9000), ftpServe) #让一个端口绑定多个程序,一般用于测试。 #socketserver.TCPServer.allow_reuse_address = True myserver.serve_forever()
本文来自博客园,作者:topass123,转载请注明原文链接:https://www.cnblogs.com/topass123/p/12817918.html