Home:ALL Converter>cherrypy and relative path in WSGI app

cherrypy and relative path in WSGI app

Ask Time:2010-11-09T10:16:51         Author:Bill Zimmerman

Json Formatter

running cherrypy with mod_wsgi on apache along with another php app. The cherrypy app is NOT mounted on root, but rather on something like 'localhost/apps/myapp' via WSGIScriptAlias in the apache config file.

In testapp.py, I have tried the following, and when I try to access localhost/apps/myapp in a browser:

app = cherrypy.tree.mount(MyApp(), '', 'settings.config') #FAILS WITH 404

and

app = cherrypy.tree.mount(MyApp(), '/apps/myapp', 'settings.config') # WORKS

The first case fails because cherrypy expects to be at the server root, instead of relative to where it is mounted via WSGI in apache.

Is there a preferred way to make cherrypy apps work relative to the path they are mounted in apache under WSGIScriptAlias?

Basically, I'll be running several cherrypy apps under several different paths, and would prefer if apache handled the dispatching (i.e. cherrypy just runs the app and doesn't worry about the relative path). This way i can avoid updating several different python files/config files everytime some of the relative paths on the server change.

Any suggestions?

btw, the cherrypy app is currently passed to the wsgi application as follows:

app = cherrypy.tree.mount(HelloWorld(), '', 'settings.config')
return app(environ, start_response)

Author:Bill Zimmerman,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/4129929/cherrypy-and-relative-path-in-wsgi-app
Robie Basak :

I am doing this, although this would require cherrypy to know the relative path:\n\nclass Dir: pass\nroot = Dir()\nroot.apps = Dir()\nroot.apps.myapp = MyApp()\ncherrypy.tree.mount(root)\n\n\nThis allows me to structure the application in any way I need. I'm using nginx and not Apache, but I don't think it'll make any difference. Although it gets a bit wordy if you're using long paths with not much else in between.\n\ncherrypy can support other dispatchers which might be better suited to what you're trying to do, or perhaps you need to write a custom one.",
2010-11-09T11:50:01
yy