如何配置Python模块" SimpleHTTPServer"这样的,例如例如foo.html
被打开http://localhost:8000/foo
被称为?
答案 0 :(得分:1)
我认为你不能把它配置成这样做......最快的方式是monkeypatching:
import SimpleHTTPServer
import os
SimpleHTTPServer.SimpleHTTPRequestHandler.translate_path = lambda self, filename: os.getcwd() + filename + ".html"
if __name__ == '__main__':
SimpleHTTPServer.test()
您可能会破坏目录列表,因此在向其添加.html之前,应检查该路径是否为目录。
您可以在此处查看更详细的示例:SimpleHTTPServer add default.htm default.html to index files
希望这有帮助
答案 1 :(得分:0)
@Juan Fco。 Roco的回答主要是我所需要的,但我最终使用的灵感来自Clokep's solution for his own blog。
最相关的部分是my fake_server.py的内容,但原始呼叫位于my fabfile.py
import os
import SimpleHTTPServer
class SuffixHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
"""
Overrides the default request handler to assume a suffixless resource is
actually an html page of the same name. Thus, http://localhost:8000/foo
would find foo.html.
Inspired by:
https://github.com/clokep/clokep.github.io/blob/source/fake_server.py
"""
def do_GET(self):
path = self.translate_path(self.path)
# If the path doesn't exist, assume it's a resource suffixed '.html'.
if not os.path.exists(path):
self.path = self.path + '.html'
# Call the superclass methods to actually serve the page.
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
SimpleHTTPServer.test(SuffixHandler)