使用WSGI提供静态文件

时间:2016-02-26 14:55:43

标签: python-2.7

我的个人网站仅包含静态文件。我想将它部署到新浪App Engine。 app引擎要求我配置index.wsgi文件。

问题是我不知道如何将domain / static / index.html与domian本身相匹配。这意味着当我输入域本身时,服务器将使用文件/static/index.html进行响应。

我不能谷歌一个很好的解决方案。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我发现了一些非常有用的东西Serve Static Content 基于此,我写了一些Python代码。问题解决了!

这是代码(index.wsgi)

import os

    MIME_TABLE = {'.txt': 'text/plain',
          '.html': 'text/html',
          '.css': 'text/css',
          '.js': 'application/javascript'
          }  

def application(environ, start_response):

    path = environ['PATH_INFO']

    if path == '/':
        path = 'static/index.html'
    else:
        path = 'static' + path

    if os.path.exists(path):
        h = open(path, 'rb')
        content = h.read()
        h.close()
        headers = [('content-type', content_type(path))]
        start_response('200 OK', headers)
        return [content]
    ''' else: return a 404 application '''

def content_type(path):

    name, ext = os.path.splitext(path)

    if ext in MIME_TABLE:
        return MIME_TABLE[ext]
    else:
        return "application/octet-stream"
相关问题