Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在wsgi测试环境中提供静态文件_Python_Wsgi - Fatal编程技术网

Python 在wsgi测试环境中提供静态文件

Python 在wsgi测试环境中提供静态文件,python,wsgi,Python,Wsgi,我正在构建一个wsgi应用程序。生产环境是apachemod_wsgi,并进行了适当的配置。为了开发,我使用wsgiref.simple_服务器在本地为wsgi应用程序提供服务。然而,我希望我的开发服务器也能以最小的开销提供静态内容。为了回答这个问题,假设我想要提供静态文件“/favicon.ico” 我遇到过“static”包:但发现文档有点缺乏,并且无法将其配置为同时服务于静态内容和我的应用程序 下面是一个示例wsgi应用程序 from cgi import parse_qs def ap

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

我遇到过“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()

注:这可能是12月份未回答问题的副本。我想添加的不仅仅是一条评论,也不想挖出一条死胡同。

让我提出这个方案。它附带了一个可精确解决此任务的函数。

在本例中,您的响应体将最终成为一个字符串,因此您每次迭代该字符串1个字符以计算长度。效率很低。去读吧,当我写一个“Hello World”程序的例子时,我并不是在追求效率。“Go read\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。如果问题没有被指出,那么肯定会有人使用你的代码,从而导致一些次优或错误的结果。此外,如果您已经克服了自己的敏感性,并且阅读了我所指的文档的实际部分,您会看到它提供了一个从WSGI应用程序返回文件的正确方法的示例。