2016-02-26 61 views
0

我的个人网站只包含静态文件。我想将它部署到新浪App Engine。应用引擎要求我配置一个index.wsgi文件。使用WSGI服务静态文件

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

我无法Google很好的解决方案。任何人都可以帮忙吗?

回答

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" 
+0

我觉得你的代码很容易受到目录遍历攻击(例如,如果有人提供'路径=” ../../等/ passwd'' - 请参阅https://stackoverflow.com/questions/6803505/does-my-code-prevent-directory-traversal了解如何清理输入路径 –