模拟Djangoweb框架

一、需求

  1.访问127.0.0.1/login,访问到login页面

  2.登陆成功,跳转到登陆后的页面

  3.登陆失败,跳转到登陆失败的页面

  4.用户账号密码验证

二、目录结构

  

三、代码

  day01.py

from wsgiref.simple_server import make_server

def foo1(request):
    f = open("egon.html", 'rb')
    data = f.read()
    f.close()
    return [data]

def foo2(request):
    f = open("alex.html", 'rb')
    data = f.read()
    return [data]

def login(request):
    f = open("login.html", 'rb')
    data = f.read()
    return [data]

def login_after(request):
    f = open("login_after.html", 'rb')
    data = f.read()
    return [data]

def login_error(request):
    f = open("login_error.html", 'rb')
    data = f.read()
    return [data]

def auth(request):
    user_union, password_union = request.get('QUERY_STRING').split('&')
    _, username = user_union.split("=")
    _, password = password_union.split("=")
    if username == 'chenwei' and password == '123':
        return login_after(request)
    else:
        return login_error(request)
def routers(): urlpattern = [ ('/login', login), ('/egon', foo1), ('/alex', foo2), ('/auth', auth), ] return urlpattern def applicaion(environ,start_response): path = environ.get('PATH_INFO') start_response('200 OK', [('Content-Type', 'text/html')]) urlpattern = routers() func = None for item in urlpattern: if path == item[0]: func = item[1] break if func: return func(environ) else: return [b'404']
t
= make_server("", 8080, applicaion) t.serve_forever()

  alex.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
</head>
<body>
<h1>welcome Alex</h1>
</body>
</html>

  egon.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
</head>
<body>
<h1>Welcome egon</h1>
</body>
</html>

  login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
</head>
<body>

<h1>登陆页面</h1>
<form action="http://localhost:8080/auth">
    <p>用户名:<input type="text" name="username"/></p>
    <p>密码:<input type="password" name="password"/></p>
    <p>提交:<input type="submit"/></p>
</form>
</body>
</html>

  login_after.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
</head>
<body>
<h1 style="color: red">登陆后页面跳转</h1>
</body>
</html>

  login_error.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
</head>
<body>
<h1 style="color: yellow">登陆失败的页面</h1>
</body>
</html>

四、代码优化

  当我们在浏览器上输入一个url,实际上是向服务器发起一次get请求,而当我们提交数据的时候,通过post发送请求,因此我们可以通过判断第一次请求是否为post请求,来判断,用户是在提交表单还是在请求网页。所以,上述的day01.py代码,auth验证可以和login整合在一起。

def login(request):
    if request.method == 'POST':
        user_union, password_union = request.get('QUERY_STRING').split('&')
        _, username = user_union.split("=")
        _, password = password_union.split("=")
        if username == 'chenwei' and password == '123':
            return login_after(request)
        else:
            return login_error(request)

    f = open("login.html", 'rb')
    data = f.read()
    return [data]

五、流程解析

    

 

posted @ 2017-08-21 14:17  看雪。  阅读(271)  评论(0编辑  收藏  举报