[Web-SIG] A more useful command-line wsgiref.simple_server?

Masklinn masklinn at masklinn.net
Thu Mar 29 12:02:46 CEST 2012


Moving here as suggested by Terry Reedy as this list may be more
interested than -ideas (note: some feedback already used to revise
the original proposal, and a very basic patch — with no tests — is
provided for the current CPython default branch)

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

I think that's a bit of a lost opportunity: the community seems to have
mostly standardized on a wsgi script providing an application callable
in its global namespace (though details may differ, mod_wsgi does not
care for the script's name and mandates an `application` callable while
e.g. gunicorn wants a Python module and the callable name must be
configured), and it would be nice if simple_server could take such a
script 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
changes would be pretty simple:

* an optional positional argument of the form `script[:app]`, the script
  is exec'd, the application (called "application" by default) is
  extracted and then mounted in simple_server. If no script is specified,
  just mount `demo_app` as before
* Add -H/--host -p/--port options to, respectively, the hostname and the
  port to bind the server to.
* The current -msimple_server uses `handle_request` and only replies once,
  to increase the usability of the CLI tool use `serve_forever` *when and
  only when the mounted application is not demo_app*. It also avoids
  opening a hardcoded example URL on launch.

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.

Attachment: patch performing the above-specified alterations, using
argparse for arguments parsing and generation of help.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: simple_server.patch
Type: application/octet-stream
Size: 1806 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/web-sig/attachments/20120329/896b448e/attachment.obj>


More information about the Web-SIG mailing list