基于Werkzeug项目DispatcherMiddleware的多应用程序调度器。

Sanic-Dispatcher的Python项目详细描述


SANIC调度员

sanic的调度程序扩展,它还充当sanic到wsgi的适配器

允许您这样做:(认真地)

fromsanicimportSanic,responsefromsanic_dispatcherimportSanicDispatcherMiddlewareControllerfromflaskimportFlask,make_response,current_appasflask_appapp=Sanic(__name__)dispatcher=SanicDispatcherMiddlewareController(app)child_sanic_app=Sanic("MyChildSanicApp")child_flask_app=Flask("MyChildFlaskApp")@app.middleware("response")asyncdefmodify_response(request,response):response.body=response.body+b"\nModified by Sanic Response middleware!"response.headers['Content-Length']=len(response.body)returnresponse@app.route("/")asyncdefindex(request):returnresponse.text("Hello World from {}".format(request.app.name))@child_sanic_app.route("/")asyncdefindex(request):returnresponse.text("Hello World from {}".format(request.app.name))@child_flask_app.route("/")defindex():app=flask_appreturnmake_response("Hello World from {}".format(app.import_name))dispatcher.register_sanic_application(child_sanic_app,'/sanicchild',apply_middleware=True)dispatcher.register_wsgi_application(child_flask_app.wsgi_app,'/flaskchild',apply_middleware=True)if__name__=="__main__":app.run(port=8001,debug=True)

安装

pip install Sanic-Dispatcher

如何使用

首先按照通常的方式制作一个sanic应用程序:

fromsanicimportSanicapp=Sanic(__name__)# This creates a sanic app

app成为您的'base'或'parent'Sanic应用程序,它将容纳Dispatcher扩展

创建调度器

fromsanic_dispatcherimportSanicDispatcherMiddlewareControllerdispatcher=SanicDispatcherMiddlewareController(app)

dispatcher是您的新调度控制器。 注意:这将引用app作为其第一个参数,但它不使用app,也不返回app

我想发送另一个sanic应用程序

app=Sanic(__name__)dispatcher=SanicDispatcherMiddlewareController(app)otherapp=Sanic("MyChildApp")dispatcher.register_sanic_application(otherapp,"/childprefix")@otherapp.route('/')asyncdefindex(request):returnresponse.text("Hello World from Child App")

浏览到url /childprefix/将调用otherapp应用程序,并调用显示“hello world from child app”的/路由

如果另一个应用程序是flask应用程序怎么办?

fromflaskimportFlask,make_responseapp=Sanic(__name__)dispatcher=SanicDispatcherMiddlewareController(app)flaskapp=Flask("MyFlaskApp")# register the wsgi_app method from the flask app into the dispatcherdispatcher.register_wsgi_application(flaskapp.wsgi_app,"/flaskprefix")@flaskapp.route('/')defindex():returnmake_response("Hello World from Flask App")

浏览到url/flaskprefix/将调用flask应用程序,并调用显示“hello world from flask app”

如果另一个应用程序是django应用程序怎么办?

importmy_django_appapp=Sanic(__name__)dispatcher=SanicDispatcherMiddlewareController(app)# register the django wsgi application into the dispatcherdispatcher.register_wsgi_application(my_django_app.wsgi.application,"/djangoprefix")

浏览到url/djangoprefix/将调用django应用程序。

我可以运行默认应用程序吗?

您在开始时创建的sanic应用程序也是默认应用程序。

当您导航到与注册的分派前缀不匹配的url时,此sanic应用程序将按照正常方式处理请求本身。

app=Sanic(__name__)dispatcher=SanicDispatcherMiddlewareController(app)otherapp=Sanic("MyChildApp")dispatcher.register_sanic_application(otherapp,"/childprefix")@app.route('/')asyncdefindex(request):returnresponse.text("Hello World from Default App")@otherapp.route('/')asyncdefindex(request):returnresponse.text("Hello World from Child App")

浏览到url /not调用任何调度程序应用程序,因此app将处理该请求本身,解析显示“hello world from default app”

我想将公共中间件应用于注册的应用程序!

别紧张!

importmy_django_appfromflaskimportFlask,make_response,current_appapp=Sanic(__name__)dispatcher=SanicDispatcherMiddlewareController(app)child_sanic_app=Sanic("MyChildSanicApp")child_flask_app=Flask("MyChildFlaskApp")@app.middleware("request")asyncdefmodify_request(request):request.headers['Content-Type']="text/plain"@app.middleware("response")asyncdefmodify_response(request,response):response.body=response.body+b"\nModified by Sanic Response middleware!"response.headers['Content-Length']=len(response.body)returnresponse@app.route("/")asyncdefindex(request):returnresponse.text("Hello World from {}".format(request.app.name))@child_sanic_app.route("/")asyncdefindex(request):returnresponse.text("Hello World from {}".format(request.app.name))@child_flask_app.route("/")defindex():app=current_appreturnmake_response("Hello World from {}".format(app.import_name))dispatcher.register_sanic_application(child_sanic_app,'/childprefix',apply_middleware=True)dispatcher.register_wsgi_application(my_django_app.wsgi.application,'/djangoprefix',apply_middleware=True)dispatcher.register_wsgi_application(child_flask_app.wsgi_app,'/flaskprefix',apply_middleware=True)

这里的关键是将apply_middleware=True传递给相关的register应用程序函数。默认情况下,对于所有注册的调度程序应用程序,apply_middleware设置为False

在本例中,sanic请求中间件modify_request将应用于所有请求,包括由在分派器上注册的应用程序处理的请求。在将请求中间件传递给任何已注册的应用程序之前,它将应用于request

在本例中,sanic响应中间件modify_response将应用于所有响应,包括由在调度程序上注册的应用程序生成的响应。响应中间件将在注册应用程序处理后应用于response

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
swing Java按钮/网格布局   java列出Google日历中的所有事件   java无效:单击API publisher test按钮后连接到后端时出错   带有内部赋值的java While循环导致checkstyle错误   java为什么trimToSize/ensureCapacity方法提供“公共”级访问?   文件输出流的java问题   ListIterator和并发修改异常的java问题   java如何使用两个URL映射   无法识别使用“./../”构造的字符串java相对路径,为什么?   首次写入remotelyclosedsocket不会触发异常,对吗?JAVA   java OneDrive REST API为文件上载提供了400个无效谓词   Java泛型、集合接口和对象类的问题   OpenSSL Java安全提供程序   jmeter java运行jmx禁用操作