为什么使用wsgiref simple_服务器?

2024-05-10 14:56:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的webapp要构建,我刚刚开始和mod_wsgi打交道。在各种教程中,第一个hello world应用程序如下所示:

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]

随后,应用程序包括一个使用wsgiref的wsgi服务器,它的一些变体是:

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()

应用程序在没有服务器的情况下运行,那么服务器的作用是什么?


Tags: 服务器应用程序wsgihelloworldserverapplicationresponse