简单实现flask route
来源:互联网 发布:明朝 知乎 编辑:程序博客网 时间:2024/06/06 09:06
import re
class NotFlask():
def __init__(self):
self.routers = [];
@staticmethod
def build_router_re(route):
route_regex = re.sub('(<\w+>)',r'(?P\1.+)',route)
route_reg_str = "^{}$".format(route_regex)
print("route_reg_str:"+route_reg_str)
return re.compile(route_reg_str)
def route(self,routh_str):
def decorator(f):
route_pattern = self.build_router_re(routh_str);
self.routers.append((route_pattern,f))
return decorator
def get_match_route(self,path):
for pattern,view_func in self.routers:
m = pattern.match(path)
if m:
return m.groupdict(),view_func
def run(self,path):
route_match = self.get_match_route(path)
if route_match:
kwargs, view_function = route_match
return view_function(**kwargs)
else:
raise ValueError('Route "{}"" has not been registered'.format(path))
if __name__ == '__main__':
app = NotFlask()
@app.route("/")
def hello_world():
return "helloworld"
@app.route("/hello/<username>")
def hello_user(username):
return "Hello {}!".format(username)
print(app.run("/"))
class NotFlask():
def __init__(self):
self.routers = [];
@staticmethod
def build_router_re(route):
route_regex = re.sub('(<\w+>)',r'(?P\1.+)',route)
route_reg_str = "^{}$".format(route_regex)
print("route_reg_str:"+route_reg_str)
return re.compile(route_reg_str)
def route(self,routh_str):
def decorator(f):
route_pattern = self.build_router_re(routh_str);
self.routers.append((route_pattern,f))
return decorator
def get_match_route(self,path):
for pattern,view_func in self.routers:
m = pattern.match(path)
if m:
return m.groupdict(),view_func
def run(self,path):
route_match = self.get_match_route(path)
if route_match:
kwargs, view_function = route_match
return view_function(**kwargs)
else:
raise ValueError('Route "{}"" has not been registered'.format(path))
if __name__ == '__main__':
app = NotFlask()
@app.route("/")
def hello_world():
return "helloworld"
@app.route("/hello/<username>")
def hello_user(username):
return "Hello {}!".format(username)
print(app.run("/"))
print(app.run("/hello/bin"))
转载:http://python.jobbole.com/80956/
原文分两章,第二章两处地方写错了一丢丢,所以自己手打了一次代码,放在这里。
侵权删
0 0
- 简单实现flask route
- flask装饰器route实现路由功能理解
- Flask 路由 route
- python-flask-route
- Flask和@app.route
- python+flask实现简单博客
- Flask-HelloWorld-09 @app.route
- flask -- route修饰器源码
- Flask源码解读(3) -- route
- flask下,实现简单博客2
- flask下,实现简单博客3
- flask下,实现简单博客4
- flask下,实现简单博客5
- flask+mongodb实现简单todolist应用
- 基于flask实现的简单博客
- flask + vue.js 实现简单todolist应用
- Flask-login 简单 Demo, 最简单的实现登录方法
- flask @app.route() 传入多个参数
- word技巧
- 获取SD卡上的全部音频文件
- VMWare Workstation 443端口被占用
- ahjesus Axure RP 7.0注册码
- Android Java层 Looper 机制
- 简单实现flask route
- Hadoop—HBase基础知识-----练习
- 关于Java堆、栈和常量池的详解
- [刷题]Find Minimum in Rotated Sorted Array
- Spring的几种注入方式
- window问题
- Hadoop—hbase安装----练习12
- iOS开发 ----- vim命令合集
- 关于双色导航栏问题