mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Merge pull request #1040 from ssbarnea/master
Fixing #1038 by allowing nice URLs.
This commit is contained in:
commit
09c9d48a6a
1 changed files with 24 additions and 5 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import logging
|
||||||
try:
|
try:
|
||||||
import SimpleHTTPServer as srvmod
|
import SimpleHTTPServer as srvmod
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
|
@ -11,19 +13,36 @@ except ImportError:
|
||||||
import socketserver # NOQA
|
import socketserver # NOQA
|
||||||
|
|
||||||
PORT = len(sys.argv) == 2 and int(sys.argv[1]) or 8000
|
PORT = len(sys.argv) == 2 and int(sys.argv[1]) or 8000
|
||||||
|
SUFFIXES = ['','.html','/index.html']
|
||||||
|
|
||||||
Handler = srvmod.SimpleHTTPRequestHandler
|
class ComplexHTTPRequestHandler(srvmod.SimpleHTTPRequestHandler):
|
||||||
|
def do_GET(self):
|
||||||
|
# we are trying to detect the file by having a fallback mechanism
|
||||||
|
r = None
|
||||||
|
for suffix in SUFFIXES:
|
||||||
|
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):
|
||||||
|
r = srvmod.SimpleHTTPRequestHandler.do_GET(self)
|
||||||
|
if r is not None:
|
||||||
|
break
|
||||||
|
logging.warning("Unable to find %s file." % self.path)
|
||||||
|
return r
|
||||||
|
|
||||||
|
Handler = ComplexHTTPRequestHandler
|
||||||
|
|
||||||
try:
|
try:
|
||||||
httpd = socketserver.TCPServer(("", PORT), Handler)
|
httpd = socketserver.TCPServer(("", PORT), Handler)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print("Could not listen on port", PORT)
|
logging.error("Could not listen on port %s" % PORT)
|
||||||
sys.exit(getattr(e, 'exitcode', 1))
|
sys.exit(getattr(e, 'exitcode', 1))
|
||||||
|
|
||||||
|
|
||||||
print("serving at port", PORT)
|
logging.info("serving at port %s" % PORT)
|
||||||
try:
|
try:
|
||||||
httpd.serve_forever()
|
httpd.serve_forever()
|
||||||
except KeyboardInterrupt as e:
|
except KeyboardInterrupt as e:
|
||||||
print("shutting down server")
|
logging.info("shutting down server")
|
||||||
httpd.socket.close()
|
httpd.socket.close()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue