Reputation: 3780
How can I configure the Python module "SimpleHTTPServer" such that e.g. foo.html
is opened when e.g. http://localhost:8000/foo
is called?
Upvotes: 0
Views: 1451
Reputation: 3780
@Juan Fco. Roco's answer mostly does what I needed, but I ended up using a solution inspired by Clokep's solution for his own blog.
The most relevant part is the content of my fake_server.py, but the originating call is in 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)
Upvotes: 0
Reputation: 1638
I don't think you can configure it to do that... the fastest way is monkeypatching:
import SimpleHTTPServer
import os
SimpleHTTPServer.SimpleHTTPRequestHandler.translate_path = lambda self, filename: os.getcwd() + filename + ".html"
if __name__ == '__main__':
SimpleHTTPServer.test()
You probably will break directory listing, so you should check if the path is a directory before adding .html to it.
You can check here more elaborated examples: SimpleHTTPServer add default.htm default.html to index files
Hope this helps
Upvotes: 1