2011-03-11 66 views
5

我有一個簡單的web應用程序來構建,並且我剛開始使用mod_wsgi。在各種教程中,第一個Hello World程序看起來像下面這樣:爲什麼使用wsgiref simple_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] 

然後,後來的應用包括使用的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() 

的應用作品,未經服務器,那麼服務器是什麼?

回答

7

我猜這個教程假設你沒有設置和運行mod_wsgi。通過這種方式,您可以從命令行運行該腳本,它將啓動運行該應用程序的wsgiref服務器,以便您無需安裝Apache和mod_wsgi即可對其進行測試。

+0

使用它的任何理由仍然一旦你有mod_wsgi運行...測試時可能有好處嗎? – jmilloy 2011-03-11 20:33:32

+1

那麼,從'wsgiref'命令行運行''dbdb'時運行它可能更容易,但除此之外我不這麼認爲。如果你把'wsgiref'啓動代碼放在'if __name__ =='__main __「:'塊中,如果你需要出於任何原因,你應該能夠輕鬆地在兩者之間切換。 – 2011-03-11 21:05:29

+0

我們是否應該在生產環境中使用wsgiref.simple_server,即不使用任何Apache或Nginx? – giga 2014-03-16 05:04:12