9

I have a simple webapp to build, and I am just beginning to mess around with mod_wsgi. In various tutorials, the first hello world app looks something like the following:

def application(environ,start_response):
   response_body = 'Hello World'
   status = '200 OK'

   response_headers = [('Content-Type', 'text/plain'),
                       ('Content-Length', str(len(response_body)))]

   start_response(status, response_headers)
   return [response_body]

Then, later the app includes a wsgi server using wsgiref, some variation of:

from wsgiref.simple_server import make_server

def application(environ, start_response):
    response_body = 'Hello World'
    status = '200 OK'

    response_headers = [('Content-Type', 'text/plain'),
                           ('Content-Length', str(len(response_body)))]

    start_response(status, response_headers)
    return [response_body]

httpd = make_server('localhost', 8000, application)
httpd.serve_forever()

The app works without the server, so what is the server for?

jmilloy
  • 7,875
  • 11
  • 53
  • 86

1 Answers1

8

I would guess the tutorial assumes you do not have mod_wsgi set up and running. This way you can run the script from the command line and it will start the wsgiref server running the application so that you can test it without having to install Apache and mod_wsgi.

Liquid_Fire
  • 6,990
  • 2
  • 25
  • 22
  • any reasons to use it still once you have mod_wsgi running... maybe benefits while testing? – jmilloy Mar 11 '11 at 20:33
  • 1
    Well, it might be easier to run it in `pdb` when running it from the command line with `wsgiref`, but apart from that I don't think so. If you put the `wsgiref` startup code in an `if __name__ == "__main__":` block, you should be able to easily switch between the two if you need to for whatever reason. – Liquid_Fire Mar 11 '11 at 21:05
  • Should we use wsgiref.simple_server in production as well, i.e. without any Apache or Nginx? – Shafiul Mar 16 '14 at 05:04
  • 1
    @giga No, you should not use `wsgiref.simple_server` in production. It is meant to be a simple reference implementation/example. It may have security and/or performance issues. Use a proper web server like Apache or nginx. – Liquid_Fire Mar 17 '14 at 16:34