ftp_server:
import os
import socketserver
import json
import hashlib
import struct
base_dir = os.getcwd()
#框架里面经常用os.path.dirname(__file__)获取当前路径
# base_dir = os.path.dirname(__file__)
userinfo = os.path.join(base_dir,"db","userinfo.txt")
#定义验证类
class Auth(object):
@staticmethod
def get_md5(usr,pwd):
hs = hashlib.md5(usr.encode())
hs.update(pwd.encode())
return hs.hexdigest()
@classmethod
def register(cls,opt_dic):
# 1.检测注册用户名是否存在
with open(userinfo,mode="r",encoding="utf-8") as f1:
for i in f1:
if i.strip().split(":")[0] == opt_dic["user"]:
return {"result":False,"info":"用户名已存在"}
# 2.当前用户可以注册
with open(userinfo, mode="a", encoding="utf-8") as f2:
strvar = f"{opt_dic['user']}:{cls.get_md5(opt_dic['user'],opt_dic['passwd'])}"
f2.write(strvar)
# 成功失败
return {"result": True, "info": "恭喜,注册成功 "}
@classmethod
def login(cls,opt_dic):
# 打开文件检测账号密码是否一致,如果成功直接返回,终止函数
with open(userinfo, mode="r", encoding="utf-8") as f1:
for i in f1:
username,password = i.strip().split(":")