routes

from utils import log
from models import Message
from models import User


# 这个函数用来保存所有的 messages
message_list = []


def template(name):
    """
    根据名字读取 templates 文件夹里的一个文件并返回
    """
    path = 'templates/' + name
    with open(path, 'r', encoding='utf-8') as f:
        return f.read()


def route_index(request):
    """
    主页的处理函数, 返回主页的响应
    """
    header = 'HTTP/1.x 210 VERY OK\r\nContent-Type: text/html\r\n'
    body = template('index.html')
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')


def route_login(request):
    """
    登录页面的路由函数
    """
    header = 'HTTP/1.x 210 VERY OK\r\nContent-Type: text/html\r\n'
    if request.method == 'POST':
        form = request.form()
        u = User(form)
        if u.validate_login():
            result = '登录成功'
        else:
            result = '用户名或者密码错误'
    else:
        result = ''
    body = template('login.html')
    body = body.replace('{{result}}', result)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')


def route_register(request):
    """
    注册页面的路由函数
    """
    header = 'HTTP/1.x 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 = '注册成功
{}
'.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_message(request): """ 消息页面的路由函数 """ log('本次请求的 method', request.method) if request.method == 'POST': form = request.form() msg = Message(form) log('post', form) message_list.append(msg) # 应该在这里保存 message_list header = 'HTTP/1.x 200 OK\r\nContent-Type: text/html\r\n' # body = '

消息版

' body = template('html_basic.html') msgs = '
'.join([str(m) for m in message_list]) body = body.replace('{{messages}}', msgs) 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.x 200 OK\r\nContent-Type: image/gif\r\n\r\n' img = header + f.read() return img # 路由字典 # key 是路由(路由就是 path) # value 是路由处理函数(就是响应) route_dict = { '/': route_index, '/login': route_login, '/register': route_register, '/messages': route_message, }

你可能感兴趣的:(routes)