开发者

why use wsgiref simple_server?

开发者 https://www.devze.com 2023-02-16 02:52 出处:网络
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:

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'

    res开发者_StackOverflow社区ponse_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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号