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?の重複である可能性があります。12月から。単なるコメント以上のものを追加したかったので、デッド スレッドを掘り下げたくありませんでした。

4

1 に答える 1

2

werkzeugパッケージを提案させてください。SharedDataMiddlewareこのタスクを正確に解決するが付属しています。

于 2013-09-05T21:03:38.577 に答える