如何让WSGI.py在静态HTML文件中提供图像?

时间:2015-03-29 20:56:15

标签: python image jpeg wsgi

为非营利组织管理基于Python(wsgi)的网站&由于历史原因,希望从一个子域提供旧的(静态)HTML。

这是wsgi.py:

(请注意,html / css文件位于static /文件夹中,其他文件位于static /的子文件夹中) (这适用于所有应用了样式的文本,但为图像提供了替代文字)

def application(env, start_response):
# some debugging queries to check what's getting passed in:
if env["QUERY_STRING"] == "env":
        start_response('200 OK', [('Content-Type',
                                 'text/html; charset=utf-8')])
        return ['<!DOCTYPE html><html><meta charset="utf-8">',
                '<title>Environment</title>',
                repr(env),
               "</html>"]
    if env["QUERY_STRING"] == "req":
        start_response('200 OK', [('Content-Type',
                                 'text/html; charset=utf-8')])
    return ['<!DOCTYPE html><html><meta charset="utf-8">',
            '<title>Incoming request dump</title>',
            "Scriptname: " + env["SCRIPT_NAME"] + "<br>",
            "Pathinfo: " + env["PATH_INFO"] + "<br>",
            "Querystring: " + env["QUERY_STRING"] + "<br>",
           "</html>"]

# the regular server code (runs OK):
    try:
        if env["PATH_INFO"] == "/": #root, no trailing '/'
            htmlfile = open("static/index.html")
            start_response('200 OK', [('Content-Type',
                                 'text/html; charset=utf-8')])
        return [htmlfile.read()]
    elif env["PATH_INFO"].endswith(".html"):
        htmlfile = open("static" + env["PATH_INFO"]) #
        start_response('200 OK', [('Content-Type',
                                 'text/html; charset=utf-8')])
        return [htmlfile.read()]
    elif env["PATH_INFO"].endswith(".css"):
        cssfile = open("static" + env["PATH_INFO"]) #
        start_response('200 OK', [('Content-Type',
                                 'text/css; charset=utf-8')])
        return [cssfile.read()]

# This generates "Internal server error:"
    elif env["PATH_INFO"].endswith(".jpg") or
         env["PATH_INFO"].endswith(".JPG"):
        jpegfile = open("static" + env["PATH_INFO"]) #
        jpegdata = jpegfile.read()
        start_response('200 OK', [('Content-Type', 'image/jpeg'),
             ('Accept-Ranges', 'bytes'),
             ('Content-Length', str(len(jpegdata))),
             ('Connection', 'close')])
        return [jpegdata]
except Exception as e:
    return ['<!DOCTYPE html><html><meta charset="utf-8"><title>Oops',
            "</title>Can't read file on server!</html>"]

- AFAICS WSGI应用程序必须返回包含字符串的iterable;图像是字节对象,但服务器正在运行Python 2.7,&amp; 'bytes'是'string'的别名 - 所以这应该不是问题。在这种情况下我无法找到有关编码的任何信息,&amp; HTTP处理八位字节。我尝试了各种各样的变化&amp;谷歌(R)这几天,但仍然卡住。如何让这个愚蠢的东西服务于图像?

2 个答案:

答案 0 :(得分:2)

这一定就够了

def application(environ, start_response):
    data = open('image.jpg', 'rb').read()
    start_response('200 OK', [('content-type': 'image/jpeg'), 
                              ('content-length', str(len(data)))])
    return [data]

也许错误不是你的想法。检查Apache的日志文件是否存在错误 - /var/log/apache2/error.log(如果您使用的是Apache)

答案 1 :(得分:0)

好吧,结果证明是一个单独的问题:Python编译器在测试中遇到'或'问题。我通过使用'env [“PATH_INFO”]。lower()'

来解决这个问题

感谢您的回复, 编