[Python-ideas] A more useful command-line wsgiref.simple_server?

Masklinn masklinn at masklinn.net
Wed Mar 28 13:37:56 CEST 2012


Currently, calling wsgiref.simple_server simply mounts the (bundled)
demo app.

I think that's a bit of a lost opportunity: as the community has mostly
standardized on a *.wsgi/wsgi.py script providing an `application` name
in its global namespace, it would be nice if wsgiref.simple_server could
take such a file as parameter and mount the application provided:

* This would allow testing that the script has no error without having
  to go through mounting it in e.g. mod_wsgi
* It would make trivial/test applications (e.g. dynamic responders to
  local JS) simpler to bootstrap as there would be no need for the
  half-dozen lines of wsgiref.simple_server bootstrapping and "hard"
  dependency on wsgiref,

    import wsgiref.simple_server

    def application(environ, start_response):
        'code'

    if __name__ == '__main__':
        httpd = make_server('', 8000, application)
        httpd.serve_forever()

  could become:

    def application(environ, start_response):
        'code'

Since wsgiref already supports `python -mwsgiref.simple_server`, the
change would be pretty simple:

* the first positional argument is the wsgi script
  if it is present it is `exec`'d, the `application` key is
    extracted from the locals and is mounted through make_server;
  if it is absent, then demo_app is mounted as before
* the second positional argument is the host, defaulting to ''
* the third positional argument is the port, defaulting to 8000

This way the current sanity test/"PHPInfo" demo app works as it did before,
but it becomes possible to very easily serve a WSGI script with almost no
overhead in the script itself.

Thoughts?



More information about the Python-ideas mailing list