一、需求

  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 == '':
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'']
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 == '':
return login_after(request)
else:
return login_error(request) f = open("login.html", 'rb')
data = f.read()
return [data]

五、流程解析

    

模拟Djangoweb框架的更多相关文章

  1. 采用dom4j和反射模拟Spring框架的依赖注入功能

    Spring的依赖注入是指将对象的创建权交给Spring框架,将对象所依赖的属性注入进来的行为.在学习了dom4j后,其实也可以利用dom4j和反射做一个小Demo模拟Spring框架的这种功能.下面 ...

  2. Mockito:一个强大的用于Java开发的模拟测试框架

    https://blog.csdn.net/zhoudaxia/article/details/33056093 介绍 本文将介绍模拟测试框架Mockito的一些基础概念, 介绍该框架的优点,讲解应用 ...

  3. 模拟spring框架注入实现原理

    这个我是参见了别人的一些东西,不是原创! 定义一些抽象的方法: package com.huxin.springinject.dao; public interface Person { public ...

  4. Python 之反射和普通方式对比(模拟Web框架)

    先模拟一个web页面的选择不同输出不同 vim day8-7.py #!/usr/bin/python # -*- coding:utf-8 -*- import home import accoun ...

  5. Django框架学习——python模拟Django框架(转载)

    原贴来源 http://wiki.woodpecker.org.cn/moin/ObpLovelyPython/AbtWebModules python实现web服务器 web开发首先要有web服务器 ...

  6. 模拟Hibernate框架的小demo

    该程序为尚学堂马士兵老师讲解,模拟了hibernate的原理,主要应用了字符串拼接,反射知识. step1,新建数据库 use jd; create table _student( _id int(1 ...

  7. Django-Web框架之创建项目和应用

    Django我们是基于python3来演示的.首先我们来安装一下django框架.使用pip3 install django安装的是最新的版本: 我们在pycharm中创建django工程.如图所示: ...

  8. .NET/C# 万能 HTTP 模拟请求框架

    我是一名 ASP.NET 程序员,专注于 B/S 项目开发.累计文章阅读量超过一千万,我的博客主页地址:https://www.itsvse.com/blog_xzz.html HttpHelper ...

  9. 【Spring系列】- 手写模拟Spring框架

    简单模拟Spring 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 前言 上次已经学习了 ...

随机推荐

  1. Oracle03--子查询

    1. 子查询 子查询也称之为嵌套子句查询. 1.1. 语法 语法上的运行使用规则: l 子查询 (内查询.嵌套子句) 在主查询之前一次执行完成.(子查询先执行) l 子查询的结果被主查询使用 (外查询 ...

  2. golang的json序列化

    json就是简单的数据交换格式,语法类似javascript的对象和列表,是最常见的后端和运行在网页上的js之间的通信格式. encoding: 编码json数据需要使用到Marshal()函数. f ...

  3. Shiro认证的另一种方式

    今天在学习shiro的时候使用另一种shiro验证的方式. 总体的思路是: (1)先在自己的方法中进行身份的验证以及给出提示信息.(前提是将自己的验证方法设为匿名可访问) (2)当验证成功之后到Shi ...

  4. MySQL5.7之多源复制&Nginx中间件(上)【转】

    有生之年系列----MySQL5.7之多源复制&Nginx中间件(上)-wangwenan6-ITPUB博客http://blog.itpub.net/29510932/viewspace-1 ...

  5. C#基础学习之StreamReader和StreamWriter

    StreamReader和StreamWriter操作字符的 FileStream操作字节的 //使用StreamReader读取文件 using (StreamReader sr=new Strea ...

  6. mysql取以当前时间为中心的任意时间段的时间戳

    例如:取当前时间后一年的时间戳 SELECT UNIX_TIMESTAMP(date_sub(curdate(),interval -1 YEAR)) SELECT UNIX_TIMESTAMP(da ...

  7. js事件兼容处理

    js封装事件处理函数,兼容ie,支持事件代理 var eventUtil = { bindEvent: function(el, type, target, callback, popgation) ...

  8. 简单的TCP接受在转发到客户端的套接口

    //功能:客服端发送tcp包,服务器接受到并打印出来,并将包转换为大写后到客户端//2015.9.10成功 #include <stdio.h>#include <sys/socke ...

  9. 使用mybatis-generator-core自动生成代码

    SSM框架可以使用mybatis-generator-core-1.3.2.jar来自动生成代码,以下是配置和使用的代码. generatorConfig.xml <?xml version=& ...

  10. Kotlin尝试

    Kotlin 是一种静态类型的编程语言,可在 Java 虚拟机上运行,也可以编译为 JavaScript 源代码.其主要发展来自位于俄罗斯圣彼得堡的 JetBrains 程序员团队.虽然语法与 Jav ...