🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
>[success] # 用来处理用户信息 ~~~ 1.用来保存当前访问 用户的session = {} 2.random_str 生成随机的sessionid 3.template 读取html文件 4.current_user 判断用户是否登录 5.route_index 请求头+body + username判断+响应返回 6.response_with_headers 定制响应头 7.route_login 处理用户登录 8.route_register 用户注册 ~~~ >[danger] ##### 代码 ~~~ import random from models import User from utils import log from utils import template # 这个函数用来保存所有的 messages message_list = [] # 用来保存当前访问 用户的session session = {} # 生成一个加密 session id def random_str(): seed = 'abcdefjsad89234hdsfkljasdkjghigaksldf89weru' s = '' for i in range(16): random_index = random.randint(0, len(seed) - 2) s += seed[random_index] return s def current_user(request): ''' 获取cookies中保存的sessionid 然后从字典中匹配对应的信息 ''' session_id = request.cookies.get('user', '') username = session.get(session_id, '游客') if username is not "游客": username = User.find(username).username return username def route_index(request): """ 主页的处理函数, 返回主页的响应 请求头+body + username判断+响应返回 """ header = 'HTTP/1.1 210 VERY OK\r\nContent-Type: text/html\r\n' body = template('index.html') username = current_user(request) body = body.replace('{{username}}', username) r = header + '\r\n' + body return r.encode(encoding='utf-8') # 定制响应头 def response_with_headers(headers, status_code=200): """ Content-Type: text/html Set-Cookie: user=gua """ header = 'HTTP/1.1 {} VERY OK\r\n'.format(status_code) header += ''.join(['{}: {}\r\n'.format(k, v) for k, v in headers.items()]) return header def redirect(location, headers=None): if headers is None: headers = { 'Content-Type': 'text/html', } headers['Location'] = location # 302 状态码的含义, Location 的作用 header = response_with_headers(headers, 302) r = header + '\r\n' + '' return r.encode(encoding='utf-8') def route_login(request): headers = { 'Content-Type': 'text/html', } username = current_user(request) if request.method == "POST": # 获取所有post 请求的参数 form = request.form() # 调用 User 函数 u = User(form) if u.validate_login(): # 查询用户名密码是否存在 user = User.find_by(username=u.username) session_id = random_str() session[session_id] = user.id log('userid',user.id) headers['Set-Cookie'] = 'user={}'.format(session_id) # headers['Set-Cookie'] = 'user_id={}'.format(user.id) result = '登录成功' log('headers response', headers) return redirect('/', headers) else: result = '用户名或者密码错误' else: result = '' body = template('login.html',username = username) header = response_with_headers(headers) r = header + '\r\n' + body # log('login', r) return r.encode(encoding='utf-8') def route_register(request): """ 注册页面的路由函数 """ header = 'HTTP/1.1 210 VERY OK\r\nContent-Type: text/html\r\n' if request.method == 'POST': form = request.form() u = User(form) if u.validate_register(): u.save() result = '注册成功<br> <pre>{}</pre>'.format(User.all()) else: result = '用户名或者密码长度必须大于2' else: result = '' body = template('register.html') body = body.replace('{{result}}', result) r = header + '\r\n' + body return r.encode(encoding='utf-8') def route_static(request): """ 静态资源的处理函数, 读取图片并生成响应返回 """ filename = request.query.get('file', 'doge.gif') path = 'static/' + filename with open(path, 'rb') as f: header = b'HTTP/1.1 200 OK\r\nContent-Type: image/gif\r\n\r\n' img = header + f.read() return img route_dict = { '/': route_index, '/login': route_login, '/register': route_register, } ~~~