質問

I am trying to run a django app and a webapp2 app together in one python interpreter. I'm using werkzeug for that as described here.

Here's my sample code.

from werkzeug.wsgi import DispatcherMiddleware
from django_app import application as djangoapp
from webapp2_app import application as webapp2app

application = DispatcherMiddleware(djangoapp, {
    '/backend':     webapp2app
})

After doing this, I would expect all requests to /backend should be treated by the webapp2 app as /. But it treats the requests as /backend. This work fines with other WSGI apps using django or flask. The problem only appears with webapp2 apps. Does anyone have any suggestions how to overcome this? Is there any other way I can achieve my purpose without using werkzeug for serving multiple WSGI apps under one domain?

役に立ちましたか?

解決

DispatcherMiddleware fabricates environments for your apps and especially SCRIPT_NAME. Django can deal with it with configuration varibale FORCE_SCRIPT_NAME = '' (docs).

With Webapp2 it's slightly more complicated. You can create subclass of webapp2.WSGIApplication and override __call__() method and force SCRIPT_NAME to desired value. So in your webapp2_app.py it could be like this

import webapp2

class WSGIApp(webapp2.WSGIApplication):

    def __call__(self, environ, start_response):
        environ['SCRIPT_NAME'] = ''
        return super(WSGIApp, self).__call__(environ, start_response)

# app = WSGIApp(...)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top