mini-WebPython Web框架程序开发(路由功能开发)
Posted ZSYL
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mini-WebPython Web框架程序开发(路由功能开发)相关的知识,希望对你有一定的参考价值。
Python Web框架程序开发(路由列表功能开发)
路由列表功能开发
1. 路由的介绍
接着上面程序的判断场景,假如咱们再处理一个个人中心的动态资源请求非常简单,再添加一个函数和更加一个分支判断就可以实现了。
framework.py 示例代码:
# 获取个人中心数据
def center():
# 状态信息
status = "200 OK"
# 响应头信息
response_header = [("Server", "PWS/1.1")]
# 1. 打开指定模板文件,读取模板文件中的数据
with open("../template/center.html", "r", encoding="utf-8") as file:
file_data = file.read()
# 2. 查询数据库,模板里面的模板变量( %content%) 替换成以后从数据库里面查询的数据
# web框架处理后的数据
# 获取当前时间, 模拟数据库内容
data = time.ctime()
response_body = file_data.replace("%content%", data)
# 这里返回的是元组
return status, response_header, response_body
以前处理动态资源请求:
# 处理动态资源请求
def handle_request(env):
# 获取动态请求资源路径
request_path = env["request_path"]
print("接收到的动态资源请求:", request_path)
if request_path == "/index.html":
# 获取首页数据
result = index()
return result
elif request_path == "/center.html":
# 获取个人中心数据
result = center()
return result
else:
# 没有找到动态资源
result = not_found()
return result
那如果咱们的框架处理的页面请求路径再多一些,比如:5个路径判断,大家可能感觉条件分支完全可以胜任,如果是40个甚至更多呢?
如果这是还是用普通的条件分支简直无法忍受。
解决办法: 可以使用路由
什么是路由?
路由就是请求的URL到处理函数的映射,也就是说提前把请求的URL和处理函数关联好。
路由列表
这么多的路由如何管理呢, 可以使用一个路由列表进行管理,通过路由列表保存每一个路由。
请求路径 | 处理函数 |
---|---|
/login.html | login函数 |
/index.html | index函数 |
/center.html | center函数 |
2. 在路由列表添加路由
framework.py 示例代码:
# 定义路由列表
route_list = [
("/index.html", index),
("/center.html", center)
]
3. 根据用户请求遍历路由列表处理用户请求
framework.py 示例代码:
# 路由列表, 列表里面的每一条记录都是一个路由
route_list = [
("/index.html", index),
("/center.html", center),
]
# 处理动态资源请求
def handle_request(env):
# 获取动态的请求资源路径
request_path = env["request_path"]
print("动态资源请求的地址:", request_path)
# 遍历路由列表,匹配请求的url
for path, func in route_list:
if request_path == path:
# 找到了指定路由,执行对应的处理函数
result = func()
return result
else:
# 没有动态资源数据, 返回404状态信息
result = not_found()
# 把处理后的结果返回给web服务器使用,让web服务器拼接响应报文时使用
return result
# # 判断请求的动态资源路径,选择指定的函数处理对应的动态资源请求
# if request_path == "/index.html":
# # 获取首页数据
# result = index()
# # 把处理后的结果返回给web服务器使用,让web服务器拼接响应报文时使用
# return result
# elif request_path == "/center.html":
# # 获取个人中心数据
# result = center()
# return result
# else:
# # 没有动态资源数据, 返回404状态信息
# result = not_found()
# # 把处理后的结果返回给web服务器使用,让web服务器拼接响应报文时使用
# return result
4. 完整代码
"""web框架的职责专门负责处理动态资源请求"""
import time
# 获取首页数据
def index():
# 状态信息
status = "200 OK"
# 响应头信息
response_header = [("Server", "PWS/1.1")]
# 1. 打开指定模板文件,读取模板文件中的数据
with open("../template/index.html", "r", encoding="utf-8") as file:
file_data = file.read()
# 2. 查询数据库,模板里面的模板变量( %content%) 替换成以后从数据库里面查询的数据
# web框架处理后的数据
# 获取当前时间, 模拟数据库内容
data = time.ctime()
response_body = file_data.replace("%content%", data)
# 这里返回的是元组
return status, response_header, response_body
# 获取个人中心数据
def center():
# 状态信息
status = "200 OK"
# 响应头信息
response_header = [("Server", "PWS/1.1")]
# 1. 打开指定模板文件,读取模板文件中的数据
with open("../template/center.html", "r", encoding="utf-8") as file:
file_data = file.read()
# 2. 查询数据库,模板里面的模板变量( %content%) 替换成以后从数据库里面查询的数据
# web框架处理后的数据
# 获取当前时间, 模拟数据库内容
data = time.ctime()
response_body = file_data.replace("%content%", data)
# 这里返回的是元组
return status, response_header, response_body
# 处理没有找到的动态资源
def not_found():
# 状态信息
status = "404 Not Found"
# 响应头信息
response_header = [("Server", "PWS/1.1")]
# web框架处理后的数据
data = "not found"
# 这里返回的是元组
return status, response_header, data
# 路由列表, 列表里面的每一条记录都是一个路由
route_list = [
("/index.html", index),
("/center.html", center),
]
# 处理动态资源请求
def handle_request(env):
# 获取动态的请求资源路径
request_path = env["request_path"]
print("动态资源请求的地址:", request_path)
# 遍历路由列表,匹配请求的url
for path, func in route_list:
if request_path == path:
# 找到了指定路由,执行对应的处理函数
result = func()
return result
else:
# 没有动态资源数据, 返回404状态信息
result = not_found()
# 把处理后的结果返回给web服务器使用,让web服务器拼接响应报文时使用
return result
# # 判断请求的动态资源路径,选择指定的函数处理对应的动态资源请求
# if request_path == "/index.html":
# # 获取首页数据
# result = index()
# # 把处理后的结果返回给web服务器使用,让web服务器拼接响应报文时使用
# return result
# elif request_path == "/center.html":
# # 获取个人中心数据
# result = center()
# return result
# else:
# # 没有动态资源数据, 返回404状态信息
# result = not_found()
# # 把处理后的结果返回给web服务器使用,让web服务器拼接响应报文时使用
# return result
装饰器方式的添加路由
1. 使用带有参数的装饰器添加路由
前面我们已经实现了路由列表,但是每次添加路由都需要手动添加来完成,接下来我们想要完成路由的自动添加,可以通过装饰器来实现,在使用装饰器对处理函数进行装饰的时候我们需要知道装饰的函数和那个请求路径进行关联,也就是说装饰器需要接收一个url参数,这样我们定义的装饰器是一个带有参数的装饰器。
示例代码:
"""web框架的职责专门负责处理动态资源请求"""
import time
# 路由列表, 列表里面的每一条记录都是一个路由
route_list = [
# ("/index.html", index),
# ("/center.html", center),
]
# 定义带有参数的装饰器
def route(path):
# 装饰器
def decorator(func):
# 当执行装饰器的时候就需要把路由添加到路由列表里面,
# 当装饰函数的时候只添加一次路由即可
route_list.append((path, func))
def inner():
result = func()
return result
return inner
# 返回一个装饰器
return decorator
# 获取首页数据
@route("/index.html") # => @decorator => index = decorator(index)
def index():
# 状态信息
status = "200 OK"
# 响应头信息
response_header = [("Server", "PWS/1.1")]
# 1. 打开指定模板文件,读取模板文件中的数据
with open("../template/index.html", "r", encoding="utf-8") as file:
file_data = file.read()
# 2. 查询数据库,模板里面的模板变量( %content%) 替换成以后从数据库里面查询的数据
# web框架处理后的数据
# 获取当前时间, 模拟数据库内容
data = time.ctime()
response_body = file_data.replace("%content%", data)
# 这里返回的是元组
return status, response_header, response_body
# 获取个人中心数据
@route("/center.html")
def center():
# 状态信息
status = "200 OK"
# 响应头信息
response_header = [("Server", "PWS/1.1")]
# 1. 打开指定模板文件,读取模板文件中的数据
with open("../template/center.html", "r", encoding="utf-8") as file:
file_data = file.read()
# 2. 查询数据库,模板里面的模板变量( %content%) 替换成以后从数据库里面查询的数据
# web框架处理后的数据
# 获取当前时间, 模拟数据库内容
data = time.ctime()
response_body = file_data.replace("%content%", data)
# 这里返回的是元组
return status, response_header, response_body
# 处理没有找到的动态资源
def not_found():
# 状态信息
status = "404 Not Found"
# 响应头信息
response_header = [("Server", "PWS/1.1")]
# web框架处理后的数据
data = "not found"
# 这里返回的是元组
return status, response_header, data
# 处理动态资源请求
def handle_request(env):
# 获取动态的请求资源路径
request_path = env["request_path"]
print("动态资源请求的地址:", request_path)
# 遍历路由列表,匹配请求的url
for path, func in route_list:
if request_path == path:
# 找到了指定路由,执行对应的处理函数
result = func()
return result
else:
# 没有动态资源数据, 返回404状态信息
result = not_found()
# 把处理后的结果返回给web服务器使用,让web服务器拼接响应报文时使用
return result
# # 判断请求的动态资源路径,选择指定的函数处理对应的动态资源请求
# if request_path == "/index.html":
# # 获取首页数据
# result = index()
# # 把处理后的结果返回给web服务器使用,让web服务器拼接响应报文时使用
# return result
# elif request_path == "/center.html":
# # 获取个人中心数据
# result = center()
# return result
# else:
# # 没有动态资源数据, 返回404状态信息
# result = not_found()
# # 把处理后的结果返回给web服务器使用,让web服务器拼接响应报文时使用
# return result
if __name__ == '__main__':
print(route_list)
2. 小结
- 使用带有参数的装饰器对处理函数进行装饰,并完成路由的添加功能。
加油!
感谢!
努力!
以上是关于mini-WebPython Web框架程序开发(路由功能开发)的主要内容,如果未能解决你的问题,请参考以下文章
mini-WebPython Web框架程序开发(logging日志)
mini-WebPython Web框架程序开发(路由功能开发)
mini-WebPython Web框架程序开发(路由功能开发)
mini-WebPython Web框架程序开发(显示数据库信息页面的开发)