diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 122d65b5..c4f5a897 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -61,10 +61,10 @@ Preview your site ----------------- Open a new terminal session and run the following commands to switch to your -``output`` directory and launch Python's built-in web server:: +``output`` directory and launch Pelican's web server:: cd ~/projects/yoursite/output - python -m SimpleHTTPServer # -m http.server if you use python3 + python -m pelican.server Preview your site by navigating to http://localhost:8000/ in your browser. diff --git a/pelican/server.py b/pelican/server.py index 60252e1f..f58ac085 100644 --- a/pelican/server.py +++ b/pelican/server.py @@ -2,30 +2,22 @@ from __future__ import print_function import os import sys import logging -try: - import SimpleHTTPServer as srvmod -except ImportError: - import http.server as srvmod # NOQA -try: - import SocketServer as socketserver -except ImportError: - import socketserver # NOQA +from six.moves import SimpleHTTPServer as srvmod +from six.moves import socketserver try: from magic import from_file as magic_from_file except ImportError: magic_from_file = None -PORT = len(sys.argv) in (2, 3) and int(sys.argv[1]) or 8000 -SERVER = len(sys.argv) == 3 and sys.argv[2] or "" -SUFFIXES = ['', '.html', '/index.html'] - class ComplexHTTPRequestHandler(srvmod.SimpleHTTPRequestHandler): + SUFFIXES = ['', '.html', '/index.html'] + def do_GET(self): # Try to detect file by applying various suffixes - for suffix in SUFFIXES: + for suffix in self.SUFFIXES: if not hasattr(self, 'original_path'): self.original_path = self.path @@ -56,19 +48,21 @@ class ComplexHTTPRequestHandler(srvmod.SimpleHTTPRequestHandler): return mimetype -Handler = ComplexHTTPRequestHandler +if __name__ == '__main__': + PORT = len(sys.argv) in (2, 3) and int(sys.argv[1]) or 8000 + SERVER = len(sys.argv) == 3 and sys.argv[2] or "" -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) - sys.exit(getattr(e, 'exitcode', 1)) + socketserver.TCPServer.allow_reuse_address = True + try: + httpd = socketserver.TCPServer((SERVER, PORT), ComplexHTTPRequestHandler) + except OSError as e: + 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) -try: - httpd.serve_forever() -except KeyboardInterrupt as e: - logging.info("Shutting down server.") - httpd.socket.close() + logging.info("Serving at port %s, server %s.", PORT, SERVER) + try: + httpd.serve_forever() + except KeyboardInterrupt as e: + logging.info("Shutting down server.") + httpd.socket.close() diff --git a/pelican/tools/templates/fabfile.py.in b/pelican/tools/templates/fabfile.py.in index a8ab586b..bcc66f6a 100644 --- a/pelican/tools/templates/fabfile.py.in +++ b/pelican/tools/templates/fabfile.py.in @@ -3,9 +3,10 @@ import fabric.contrib.project as project import os import shutil import sys -import SimpleHTTPServer import SocketServer +from pelican.server import ComplexHTTPRequestHandler + # Local path configuration (can be absolute or relative to fabfile) env.deploy_path = 'output' DEPLOY_PATH = env.deploy_path @@ -51,7 +52,7 @@ def serve(): class AddressReuseTCPServer(SocketServer.TCPServer): allow_reuse_address = True - server = AddressReuseTCPServer(('', PORT), SimpleHTTPServer.SimpleHTTPRequestHandler) + server = AddressReuseTCPServer(('', PORT), ComplexHTTPRequestHandler) sys.stderr.write('Serving on port {0} ...\n'.format(PORT)) server.serve_forever()