最近有很长一段时间没有再继续跟大家分享python的学习技术心得,实属抱歉。
今天给小伙伴们分享一个用python中的web框架bottle来进行web开发,简易书写,模拟登陆,获取并保存用户的一个登陆名称及密码。
Bottle框架学习地址:https://www.cnblogs.com/horror/p/9494413.html
注意:在实际的用户系统中,我们绝对不能明文保存用户密码,这是对用户的不负责。
1、新建user.py文件,写登录逻辑处理函数
#user.pyimport osif not os.path.exists('./userinfo.txt'): open('./userinfo.txt', 'w', encoding = 'utf-8').close() def read_user(username, password): userinfo = dict() with open('./userinfo.txt', 'r') as fob: for line in fob.readlines(): uname = line.strip().split('=>')[0] try: pwd = line.strip().split('=>')[1] userinfo[uname] = pwd except: print('\033[1;31;40m 严重:用户信息文件格式错误,系统无法运行 \033[0m') exit(1) if username not in userinfo: return False if userinfo[username] == password: return True return False
总结一下read_user函数的功能就是根据传入的用户名和密码,判断是否和userinfo.txt中的账号密码匹配,如果正确返回True,反之。
在userinfo.txt使用=>标识分割用户名和密码。
#main.pyfrom bottle import run,route,template,requestfrom user import read_user@route("/login", method = 'get')def index(): return template('login')@route('/login', method = 'post')def index(): username = request.forms.get('username') password = request.forms.get('password') if read_user(username, password): return '登录成功' return '账号密码错误' run(host = 'localhost', port = 80, debug = True, reloader = True)
我们在main.py中导入user Module,然后修改判断逻辑
i
f username == 'admin' and password == 'root'为if read_user(username, password)
先手动在userinfo.txt中写入一些测试的账号和密码
然后模拟登陆系统,
console中看提交的网络请求
当然,到目前为止还是一个玩具,还有很多要完善的东西。
前端传输用户信息加密
后端加密保存用户密码
更友好的用户登录界面
螺丝帽验证码
注册用户
识别用户登录状态
退出登录
修改密码
………………
接下来我们首先实现完善系统,实现注册,登录,状态保持,退出等功能。小伙伴们敬请期待吧!
点击头条,点击右下角“我的”
在个人界面点击关注,如果你关注了,点击“小杆货”就可以了