模拟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 == '':
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框架的更多相关文章
- 采用dom4j和反射模拟Spring框架的依赖注入功能
Spring的依赖注入是指将对象的创建权交给Spring框架,将对象所依赖的属性注入进来的行为.在学习了dom4j后,其实也可以利用dom4j和反射做一个小Demo模拟Spring框架的这种功能.下面 ...
- Mockito:一个强大的用于Java开发的模拟测试框架
https://blog.csdn.net/zhoudaxia/article/details/33056093 介绍 本文将介绍模拟测试框架Mockito的一些基础概念, 介绍该框架的优点,讲解应用 ...
- 模拟spring框架注入实现原理
这个我是参见了别人的一些东西,不是原创! 定义一些抽象的方法: package com.huxin.springinject.dao; public interface Person { public ...
- Python 之反射和普通方式对比(模拟Web框架)
先模拟一个web页面的选择不同输出不同 vim day8-7.py #!/usr/bin/python # -*- coding:utf-8 -*- import home import accoun ...
- Django框架学习——python模拟Django框架(转载)
原贴来源 http://wiki.woodpecker.org.cn/moin/ObpLovelyPython/AbtWebModules python实现web服务器 web开发首先要有web服务器 ...
- 模拟Hibernate框架的小demo
该程序为尚学堂马士兵老师讲解,模拟了hibernate的原理,主要应用了字符串拼接,反射知识. step1,新建数据库 use jd; create table _student( _id int(1 ...
- Django-Web框架之创建项目和应用
Django我们是基于python3来演示的.首先我们来安装一下django框架.使用pip3 install django安装的是最新的版本: 我们在pycharm中创建django工程.如图所示: ...
- .NET/C# 万能 HTTP 模拟请求框架
我是一名 ASP.NET 程序员,专注于 B/S 项目开发.累计文章阅读量超过一千万,我的博客主页地址:https://www.itsvse.com/blog_xzz.html HttpHelper ...
- 【Spring系列】- 手写模拟Spring框架
简单模拟Spring 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 前言 上次已经学习了 ...
随机推荐
- python 操作 memcache
目录 Memcached Memcached安装 python操作Memcached Memcache模块常用方法 Memcached Memcached是一个高性能的分布式内存对象缓存系统,用于动态 ...
- 63、使用Timer类来实现定时任务
定时任务 定时任务就是让计算机自动的每隔一段时间执行的代码.比如要实现这样的一个功能:让计算机每隔5秒钟,在控制台打印一个www.monkey1024.com可以使用java.util包下的Timer ...
- Django中html里的分页显示
分页一(very low) 因为数据量过大,而又想直观便捷的查看数据,进而通过分页显示就可以完成这项工作 app中views.py LIST=[] #全局定义一个LIST for i in range ...
- 线段树(dfs序建树加区间更新和单点查询)
题目链接:https://cn.vjudge.net/contest/66989#problem/J 记录一下这道折磨了我一天的题,.... 具体思路: 具体关系可通过dfs序建树,但是注意,在更新以 ...
- 南京邮电大学 CTF 逆向部分 Writeup
Hello,RE! 提示 IDA 中按 R . Google 到 IDA 中 R 快捷键是 Character ,转为字符串. 丢进 IDA(虽然我并不会使用 IDA 有个 strcmp 函数,比较 ...
- MS Office CVE-2015-1641 恶意 Exploit 样本分析
MS Office CVE-2015-1641 恶意 Exploit 样本分析 在对最近的一个恶意 MS Office 文档样本进行分析时,我们发现了一些有趣的特性.这个文档利用 CVE-2015-1 ...
- .NET 4.5 Task异步编程学习资料
参考资料: 1. http://www.cnblogs.com/heyuquan/archive/2013/04/18/3028044.html
- python基础--logging模块
很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...
- ThinkPHP文件目录说明
1.ThinkPHP文件包下目录结构说明 2.ThinkPHP文件目录下文件说明 3.Conf目录下 4.Library目录
- qlserver排序规则在全角与半角处理中的应用
--1.查询区分全角与半角字符--测试数据DECLARE @t TABLE(col varchar(10))INSERT @t SELECT 'aa'UNION ALL SELECT 'Aa'UNIO ...