Python 2.7 使用WSGI服务静态文件

Python 2.7 使用WSGI服务静态文件,python-2.7,Python 2.7,我的个人网站只包含静态文件。我想将其部署到新浪应用程序引擎。应用程序引擎要求我配置index.wsgi文件 问题是我不知道如何将domain/static/index.html与domian本身匹配。这意味着当我输入域本身时,服务器将使用文件/static/index.html进行响应 我无法用谷歌搜索一个好的解决方案。有人能帮忙吗?我发现了一些非常有用的东西 基于此,我编写了一些Python代码。问题解决了 下面是代码(index.wsgi) 我认为您的代码容易受到目录遍历攻击(例如,如果有人

我的个人网站只包含静态文件。我想将其部署到新浪应用程序引擎。应用程序引擎要求我配置index.wsgi文件

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


我无法用谷歌搜索一个好的解决方案。有人能帮忙吗?

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

下面是代码(index.wsgi)


我认为您的代码容易受到目录遍历攻击(例如,如果有人提供
path='../../etc/passwd'
,请参阅以了解如何清理输入路径)
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"