为学习目的而构建的bumbopython Web框架。
bumbo-izz的Python项目详细描述
Bumbo:Python为学习而构建的Web框架
Bumbo是一个用于学习的Python web框架。在
它是一个WSGI框架,可以与任何WSGI应用服务器(如Gunicorn)一起使用。在
安装
pip install bumbo
如何使用它
^{pr2}$
单元测试
编写单元测试的推荐方法是使用pytest。有两个内置固定装置
在使用Bumbo编写单元测试时可能需要使用。第一个是app
,它是主API
类的一个实例:
deftest_route_overlap_throws_exception(app):@app.route("/")defhome(req,resp):resp.text="Welcome Home."withpytest.raises(AssertionError):@app.route("/")defhome2(req,resp):resp.text="Welcome Home2."
另一个是client
,您可以使用它向处理程序发送HTTP请求。它是根据著名的requests改编的,应该会让人觉得很熟悉:
deftest_parameterized_route(app,client):@app.route("/{name}")defhello(req,resp,name):resp.text=f"hey {name}"assertclient.get("http://testserver/matthew").text=="hey matthew"
模板
模板的默认文件夹是templates
。您可以在初始化主API()
类时更改它:
app=API(templates_dir="templates_dir_name")
然后您可以像在处理程序中那样使用该文件夹中的HTML文件:
@app.route("/show/template")defhandler_with_template(req,resp):resp.html=app.template("example.html",context={"title":"Awesome Framework","body":"welcome to the future!"})
静态文件
与模板一样,静态文件的默认文件夹是static
,您可以覆盖它:
app=API(static_dir="static_dir_name")
然后您可以在HTML文件中使用此文件夹中的文件:
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>{{title}}</title><linkhref="/static/main.css"rel="stylesheet"type="text/css"></head><body><h1>{{body}}</h1><p>This is a paragraph</p></body></html>
中间件
您可以通过继承bumbo.middleware.Middleware
类并重写其两个方法来创建自定义中间件类
在每个请求之前和之后调用:
frombumbo.apiimportAPIfrombumbo.middlewareimportMiddlewareapp=API()classSimpleCustomMiddleware(Middleware):defprocess_request(self,req):print("Before dispatch",req.url)defprocess_response(self,req,res):print("After dispatch",req.url)app.add_middleware(SimpleCustomMiddleware)
标签:
- 项目
推荐PyPI第三方库
单元测试
编写单元测试的推荐方法是使用pytest。有两个内置固定装置
在使用Bumbo编写单元测试时可能需要使用。第一个是app
,它是主API
类的一个实例:
deftest_route_overlap_throws_exception(app):@app.route("/")defhome(req,resp):resp.text="Welcome Home."withpytest.raises(AssertionError):@app.route("/")defhome2(req,resp):resp.text="Welcome Home2."
另一个是client
,您可以使用它向处理程序发送HTTP请求。它是根据著名的requests改编的,应该会让人觉得很熟悉:
deftest_parameterized_route(app,client):@app.route("/{name}")defhello(req,resp,name):resp.text=f"hey {name}"assertclient.get("http://testserver/matthew").text=="hey matthew"
模板
模板的默认文件夹是templates
。您可以在初始化主API()
类时更改它:
app=API(templates_dir="templates_dir_name")
然后您可以像在处理程序中那样使用该文件夹中的HTML文件:
@app.route("/show/template")defhandler_with_template(req,resp):resp.html=app.template("example.html",context={"title":"Awesome Framework","body":"welcome to the future!"})
静态文件
与模板一样,静态文件的默认文件夹是static
,您可以覆盖它:
app=API(static_dir="static_dir_name")
然后您可以在HTML文件中使用此文件夹中的文件:
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>{{title}}</title><linkhref="/static/main.css"rel="stylesheet"type="text/css"></head><body><h1>{{body}}</h1><p>This is a paragraph</p></body></html>
中间件
您可以通过继承bumbo.middleware.Middleware
类并重写其两个方法来创建自定义中间件类
在每个请求之前和之后调用:
frombumbo.apiimportAPIfrombumbo.middlewareimportMiddlewareapp=API()classSimpleCustomMiddleware(Middleware):defprocess_request(self,req):print("Before dispatch",req.url)defprocess_response(self,req,res):print("After dispatch",req.url)app.add_middleware(SimpleCustomMiddleware)
- 项目
标签: