forked from github/pelican
Minor changes to server.py
- Log original path name rather than last path name - Use for-else construct to avoid use of flag variable - Make log messages consistent
This commit is contained in:
parent
6616ed8816
commit
79548c3bf9
1 changed files with 15 additions and 11 deletions
|
|
@ -19,21 +19,25 @@ SUFFIXES = ['', '.html', '/index.html']
|
|||
|
||||
class ComplexHTTPRequestHandler(srvmod.SimpleHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
# we are trying to detect the file by having a fallback mechanism
|
||||
found = False
|
||||
# Try to detect file by applying various suffixes
|
||||
for suffix in SUFFIXES:
|
||||
if not hasattr(self,'original_path'):
|
||||
if not hasattr(self, 'original_path'):
|
||||
self.original_path = self.path
|
||||
|
||||
self.path = self.original_path + suffix
|
||||
path = self.translate_path(self.path)
|
||||
|
||||
if os.path.exists(path):
|
||||
srvmod.SimpleHTTPRequestHandler.do_GET(self)
|
||||
logging.info("Found: %s" % self.path)
|
||||
found = True
|
||||
logging.info("Found `%s`." % self.path)
|
||||
break
|
||||
logging.info("Tried to find file %s, but it doesn't exist. ", self.path)
|
||||
if not found:
|
||||
logging.warning("Unable to find file %s or variations.", self.path)
|
||||
|
||||
logging.info("Tried to find `%s`, but it doesn't exist.",
|
||||
self.path)
|
||||
else:
|
||||
# Fallback if there were no matches
|
||||
logging.warning("Unable to find `%s` or variations.",
|
||||
self.original_path)
|
||||
|
||||
Handler = ComplexHTTPRequestHandler
|
||||
|
||||
|
|
@ -41,13 +45,13 @@ socketserver.TCPServer.allow_reuse_address = True
|
|||
try:
|
||||
httpd = socketserver.TCPServer((SERVER, PORT), Handler)
|
||||
except OSError as e:
|
||||
logging.error("Could not listen on port %s, server %s", PORT, SERVER)
|
||||
logging.error("Could not listen on port %s, server %s.", PORT, SERVER)
|
||||
sys.exit(getattr(e, 'exitcode', 1))
|
||||
|
||||
|
||||
logging.info("Serving at port %s, server %s", PORT, SERVER)
|
||||
logging.info("Serving at port %s, server %s.", PORT, SERVER)
|
||||
try:
|
||||
httpd.serve_forever()
|
||||
except KeyboardInterrupt as e:
|
||||
logging.info("Shutting down server")
|
||||
logging.info("Shutting down server.")
|
||||
httpd.socket.close()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue