【问题标题】:why use wsgiref simple_server?为什么使用 wsgiref simple_server?
【发布时间】:2011-07-13 17:49:53
【问题描述】:

我有一个简单的 web 应用程序要构建,我刚刚开始搞乱 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()

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

【问题讨论】:

    标签: python mod-wsgi wsgi wsgiref


    【解决方案1】:

    我猜本教程假设您没有设置和运行 mod_wsgi。这样您就可以从命令行运行脚本,它会启动运行应用程序的wsgiref 服务器,这样您就可以测试它而无需安装 Apache 和 mod_wsgi。

    【讨论】:

    • 运行 mod_wsgi 后还有什么理由继续使用它...也许在测试时会受益?
    • 好吧,当使用wsgiref 从命令行运行它时,在pdb 中运行它可能更容易,但除此之外我不这么认为。如果您将wsgiref 启动代码放在if __name__ == "__main__": 块中,则无论出于何种原因需要,您都应该能够轻松地在两者之间切换。
    • 我们是否应该在生产环境中也使用 wsgiref.simple_server,即不使用任何 Apache 或 Nginx?
    • @giga 不,你不应该在生产中使用wsgiref.simple_server。它是一个简单的参考实现/示例。它可能存在安全和/或性能问题。使用适当的网络服务器,如 Apache 或 nginx。
    猜你喜欢
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 2011-08-30
    • 2010-11-01
    相关资源
    最近更新 更多