3

我正在构建一个 wsgi 应用程序。生产环境是 Apache mod_wsgi 并进行了适当的配置。对于开发,我使用 wsgiref.simple_server 在本地为 wsgi 应用程序提供服务。但是,我希望我的开发服务器也能以最小的开销提供静态内容。为了回答这个问题,假设我想提供静态文件“/favicon.ico”。

我遇到了“静态”包:http: //lukearno.com/projects/static/ 但发现文档有点缺乏,并且无法将其配置为同时提供静态内容和我的应用程序。

这是一个示例 wsgi 应用程序。

from cgi import parse_qs

def application(environ, start_response):
    qs = parse_qs(environ['QUERY_STRING'])

    response_body, content_type = ('Hello World', ('Content-type', 'text/plain'))

    content_length = 0
    for s in response_body:
        content_length += len(s)

    status = '200 OK'
    response_headers = [content_type,
                        ('Content-Length', str(content_length))]
    start_response(status, response_headers)

    return response_body



if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    # Instantiate the WSGI web server.
    httpd = make_server('192.168.1.1', # The host name.
                        8080, # A port number where to wait for the request.
                        application # Our application object name, in this case a function.
                        )
    httpd.serve_forever()

注意:这可能与未回答的问题Serving static content with python wsgiref? 从十二月开始。我想添加的不仅仅是评论,也不想挖掘死线。

4

1 回答 1

2

让我提出werkzeug包。它带有一个SharedDataMiddleware可以精确解决此任务的问题。

于 2013-09-05T21:03:38.577 回答