[Web-SIG] On HTTP Servers and WSGI (was "WSGI in standard library")

Ian Bicking ianb at colorstudy.com
Tue Feb 7 02:53:49 CET 2006


Christian Wyglendowski wrote:
> Robert Brewer said (in reference to CherryPy's WSGI server):
> 
>>>However, one thing CP's WSGI server currently does *not* have is the
>>>ability for the user to configure SCRIPT_NAME. Christian Wyglendowski is
>>>working on a patch for that at the moment
> 
> 
> Ian Bicking said:
> 
>>I think this is a separate issue.  The HTTP server should always set 
>>SCRIPT_NAME to ''.
> 
> 
> If it is a full blown WSGI compliant HTTP server, why should it do that? 
>   Shouldn't it support mounting applications at various points 
> (SCRIPT_NAME locations)?  That makes sense to me at least.

It doesn't really need to do that, it's easy to do that in the WSGI 
application itself.  For instance:

def dispatch(app_map):
     app_map = app_map.items()
     app_map.sort(lambda a, b: -cmp(len(a[0]), len(b[0])))
     def application(environ, start_response):
         path_info = environ.get('PATH_INFO', '')
         for app_prefix, app in app_map:
             app_prefix = app_prefix.rstrip('/')+'/'
             if path_info.startswith(app_prefix):
                 environ['SCRIPT_NAME'] += app_prefix[:-1]
                 environ['PATH_INFO'] = environ.get(
                     'PATH_INFO', '')[len(app_prefix)-1:]
                 return app(environ, start_response)
         else:
             start_response('404 Not Found', [])
             return []
     return application

dispatching_app = dispatch({
   '/blog': my_blog_app,
   '/': root_app,
   '/admin': admin_app,
   })

I can understand this is a common desire and it is not obvious that you 
can do this, so it might be useful to include some middleware like this. 
  But the server itself does not need to do this.  And there's lots of 
ways you might want to do this (e.g., virtual hosts, which do the same 
basic thing but match against environ['HTTP_HOST']).  Paste includes 
code along these lines in paste.urlmap: 
http://pythonpaste.org/module-paste.urlmap.html

-- 
Ian Bicking  |  ianb at colorstudy.com  |  http://blog.ianbicking.org


More information about the Web-SIG mailing list