模拟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 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 前言 上次已经学习了 ...
随机推荐
- 【Eclipse】eclipse生成类图、类交互图、包依赖图
今天,在修改毕设论文的时候需要画类图,系统已经开发完成,如果手动拿PowerDesigner画类图太浪费时间,于是通过网上查阅资料发现eclipse可以集成一个插件生成类图,也可以生成包图.现在做记录 ...
- _csv.Error: line contains NULL byte
原因是表格保存时扩展名为 xls,而我们将其改为csv文件通常是重命名: 解决方法只需把它另存为 csv 文件.
- thinkphp报错Call to undefined method app\index\controller\Index::fetch()
因为要写一个系统,所以又重新下载了thinkphp,然后安装了一下.回忆起这个问题很容易让新手朋友费解.会出现如下报错:Call to undefined method app\index\contr ...
- 83.Linux之ubuntu-14.04.4-desktop-amd64安装
QQ(1044233591) 一.软件下载 二.安装 1.上一节已经安装好了VMware10.0.4软件,双击桌面VMware Workstation软件图标,出现VMware软件界面,点击" ...
- 解决 Windows 环境 Git Bash 无法识别 Composer 命令的问题
思路 模拟 Linux,复制一个 composer 文件到 Git Bash 的 /usr 的子目录,并赋予执行权限. 解决 首先,请确定你的 composer.phar 文件路径.我的是: /d/w ...
- RocketMQ使用
RocketMQ是阿里巴巴在2012年开源的分布式消息中间件,目前已经捐赠给Apache基金会,并于2016年11月成为 Apache 孵化项目. 中间件是一类连接软件组件和应用的计算机软件,它包括一 ...
- 数据库-mysql数据连接
一:Mysql 连接的使用 在前几章节中,我们已经学会了如果在一张表中读取数据,这是相对简单的,但是在真正的应用中经常需要从多个数据表中读取数据. 本章节我们将向大家介绍如何使用 MySQL 的 JO ...
- Python元组与字典详解
Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 如下实例: tup ...
- bootstrap表单按回车会自动刷新页面的问题
想给form表单增加回车自动提交的功能 $('#password').keydown(function(event){ if (event.keyCode == 13) $('#login').cli ...
- 洛谷P2822组合数问题
传送门啦 15分暴力,但看题解说暴力分有30分. 就是找到公式,然后套公式.. #include <iostream> #include <cstdio> #include & ...