当前位置:网站首页>60、wsgiref手写web框架+jinja2模块初识
60、wsgiref手写web框架+jinja2模块初识
2022-07-17 07:24:00 【永久_小玖】
一、wsgiref模块编写
- wsgiref模块.py
from wsgiref.simple_server import make_server
def index(env):
return 'hello index'
def login(env):
return 'hello login'
def error(env):
return '404 error'
# url与函数的对应关系
urls = [
('/index', index),
('/login', login),
('/error', error)
]
def run(env, response):
""" :param env: 请求相关的所有数据 :param response: 响应相关的所有数据 :return: 返回给浏览器的数据 """
print(env) # 大字典 wsgiref模块帮你处理好http格式的数据
current_path = env.get('PATH_INFO')
response('200 OK', []) # 相应首行, 响应头
# 定义一个变量 存储匹配到的函数名
func = None
for url in urls:
if current_path == url[0]:
# 将url对应的函数名给func
func = url[1]
break # 匹配到一个后应该立即结束for循环
# 判断for是否有值
if func:
res = func(env)
else:
res = error(env)
return [res.encode('utf-8')]
if __name__ == '__main__':
server = make_server('127.0.0.1', 8080, run)
""" 会实时监控127.0.0.1:8080地址, 只要有客户端来了 都会交给run函数处理(加括号触发run函数的运行) flask启动源码 make_server('127.0.0.1', 8080, obj) 会启动__call__ """
server.serve_forever() # 启动服务端
- 优化就是将不同的功能拆开不同的py文件, 就已经有django的雏形了
二、模板语法jinja2
pip install janja2
''' 模板语法是在后端起作用的, 将处理好的模板给前端 '''
# 模板语法(类似于python)
{
{
user}}
{
{
user.get('username')}}
{
{
user.age}}
{
{
user['app']}}
三、自定义建议版本web框架流程图
# wsgires 模块
1、 请求来的时候解析http格式的数据, 封装成大字典
2、 响应走的时候给数据打包成符合http格式, 在返回给浏览器
- 流程图

相关连接(笔记来自于视频课程的归类整理):
[1]: https://www.bilibili.com/video/BV1QE41147hU?p=17
[2]: https://www.cnblogs.com/Dominic-Ji/p/11093137.html
[3]: https://zhuanlan.zhihu.com/p/114200361
边栏推荐
- 聊聊分布式锁
- 地址监控API:如何追溯与监控Uniswap黑客地址
- 在VSCode中设置settings.json
- Deep learning 7 deep feedforward network
- Database review -- database recovery technology
- [C# Console]-C# 控制臺類
- Code learning (deamnet) CVPR | adaptive consistency prior based deep network for image learning
- Wvppro-zlm-gb21818-camera
- Array exercise 3
- [C# Console]-C# 控制台类
猜你喜欢

【flask入门系列】请求钩子与上下文

总结的太好了!终于有人把SQL的各种连接Join都讲明白了

Real case: how to check the soaring usage of CPU after the system goes online?

Redis message subscription

畅玩JVM——关于GC垃圾回收必须要掌握的知识

Redis新数据类型——Bitmaps

ObjectARX--自定义圆的实现

WPF 三维应用搭建(基础)

How to check whether the app has user information and data leakage vulnerabilities

Unity: WebGL发布后在浏览器上运行时窗口大小自适应
随机推荐
关于快慢指针的理解
一款关于日常习惯打卡的小程序
[C # console] - C # console class
Obtain the home location through IP
Visual studio production environment configuration scheme: slowcheetah
在VSCode中设置settings.json
Unity: WebGL发布后在浏览器上运行时窗口大小自适应
Redis常用数据类型——哈希(Hash)和有序集合 Zset(sorted set)
写代码遇到Qt相关问题
openpyxl跨工作簿复制sheet页
Dark horse programmer - software testing -16 stage 3 - function testing -175-198, URL composition introduction, request content and composition description line function test and database, URL composi
Stm32f103c8t6 hardware IIC control 4-pin 0.96 inch OLED display
Openpyxl copy sheet pages across workbooks
The core problem of concurrent programming
A small program about daily habit clock in
Consul服务注册与发现
OpenFeign服务接口调用
Do online usdt and usdc want to be short? Take you to find out | tokenview
Redis cache avalanche, penetration, breakdown
By voting for the destruction of STI by Dao, seektiger is truly community driven