当前位置:网站首页>Mini web framework: template replacement and routing list function development | dark horse programmer
Mini web framework: template replacement and routing list function development | dark horse programmer
2022-06-22 19:35:00 【Dark horse programmer official】
One 、 Template replacement function development
1. Read the stock information template file
framework.py Sample code :
# Get home page data
def index():
# Response state
status = "200 OK";
# Response head
response_header = [("Server", "PWS2.0")]
# Open template file , Reading data
with open("template/index.html", "r") as file:
file_data = file.read()
2. Replace template variables with analog data
framework.py Sample code :
# Get home page data
def index():
# Response state
status = "200 OK";
# Response head
response_header = [("Server", "PWS2.0")]
# 1. Open template file , Reading data
with open("template/index.html", "r") as file:
file_data = file.read()
# Processed data , Query from database
data = time.ctime()
# 2. Replace the template in the template file
result = file_data.replace("{%content%}", data)
return status, response_header, result
Two 、 Routing list function development
1. Introduction to routing
Then the judgment scenario of the above program , If we deal with the dynamic resource request of a personal center, it is very simple , Add one more function and one more branch judgment to realize .
framework.py Sample code :
# Get personal center data
def center():
# Response state
status = "200 OK";
# Response head
response_header = [("Server", "PWS2.0")]
# Open template file , Reading data
with open("template/center.html", "r") as file:
file_data = file.read()
# Processed data , Query from database
data = time.ctime()
# Replace the template in the template file
result = file_data.replace("{%content%}", data)
return status, response_header, result
# Handle dynamic resource requests
def handle_request(env):
# Get the dynamic request resource path
request_path = env["request_path"]
print(" Dynamic resource request received :", request_path)
if request_path == "/index.html":
# Get home page data
result = index()
return result
elif request_path == "/center.html":
# Get personal center data
result = center()
return result
else:
# Dynamic resource not found
result = not_found()
return result
If our framework handles more page request paths , such as :5 Path judgment , You may feel that the conditional branch is completely competent , If it is 40 Even more ? If this is still a normal conditional branch, it is simply unbearable .
terms of settlement : You can use routing
What is routing ?
Routing is the request URL Mapping to handler , That is to say, send the request in advance URL It is well associated with the handler .
The routing list
How to manage so many routes , You can use a routing list to manage , Save each route through the route list .
| Request path | Processing function |
|---|---|
| /login.html | login function |
| /index.html | index function |
| /center.html | center function |
2. Add a route to the route list
framework.py Sample code :
# Define routing list
route_list = [
("/index.html", index),
("/center.html", center)
]
3. Traverse the routing list according to the user request to process the user request
framework.py Sample code :
# Handle dynamic resource requests
def handle_request(env):
# Get the dynamic request resource path
request_path = env["request_path"]
print(" Dynamic resource request received :", request_path)
# Traverse the routing list , Select the function to execute
for path, func in route_list:
if request_path == path:
result = func()
return result
else:
# Dynamic resource not found
result = not_found()
return result
# if request_path == "/index.html":
# # Get home page data
# result = index()
# return result
# elif request_path == "/center.html":
# # Get personal center data
# result = center()
# return result
# else:
# # Dynamic resource not found
# result = not_found()
# return result
边栏推荐
- Cluster, distributed and microservice concepts and differences
- shell脚本详解(七)——正则表达式、sort、uniq、tr
- How to choose smart home? Take a look at this shopping guide
- A homekit enabled camera? Zhiting IPC camera IC1 unpacking experience
- Pat a 1093 count Pat's (25 points)
- Ts as const
- ssh免密码登录
- jniLibs.srcDirs = [‘libs‘]有什么用?
- In the first half of the year, there were 7 new unicorns in this field, and the capital scrambled to enter the market
- Digital commerce cloud: analyze the design idea of B2B2C multi-user mall system architecture, and open a new era of intelligent mall
猜你喜欢

实验七 触发器

加工制造业智慧采购系统解决方案:助力企业实现全流程采购一体化协同

故障分析 | 从 data_free 异常说起

Active Directory用户登录报告

数商云:数字化供应链系统搭建,赋能企业实现物流供应链的优化升级
Notes on Combinatorics (V) chains in distributive lattice

what? Homekit, Micah, aqara and other ecosystems can also be linked with tmall elf ecology through zhiting?

下拉刷新及上拉加载更多的ListView

Message Oriented Middleware (I) MQ explanation and comparison of four MQS

STM32 control matrix key, Hal library, cubemx configuration
随机推荐
Weizhi technology appeared in the Western Digital Expo, and the space-time AI technology was highly recognized
canvas给图片画框框
故障分析 | 从 data_free 异常说起
SSH password free login
shell脚本详解(四)——循环语句之while循环和until循环(附加例题及解析)
2年狂赚178亿元,中国游戏正在“收割”老外
Shell script explanation (II) -- conditional test, if statement and case branch statement
有效的括号
jniLibs.srcDirs = [‘libs‘]有什么用?
84.(cesium篇)cesium模型在地形上运动
Active directory user logon Report
Flutter series -flutter route management
Zynq UltraScale + RFSoC ZCU111 RF时钟树学习 1
Iplook and SBC establish long-term cooperation
Thread pool: reading the source code of threadpoolexcutor
机械设备行业数字化供应链集采平台解决方案:优化资源配置,实现降本增效
mini-Web框架:模板替换与路由列表功能开发 | 黑马程序员
加工制造业智慧采购系统解决方案:助力企业实现全流程采购一体化协同
STM32控制矩阵按键,HAL库,cubeMX配置
IPLOOK作为O-RAN联盟会员,将共同促进5G产业发展