From 786a209d2b1156095048087cb727a41df7562f14 Mon Sep 17 00:00:00 2001 From: Tom Yubing Dong Date: Thu, 26 Dec 2013 22:30:54 +0800 Subject: [PATCH] `ctrl+c` now correctly terminates `fab serve` Previously `ctrl+c` a `fab serve` wouldn't necessarily terminate the web server. Even if it does, re-using the command `fab serve` might result in the following error: ``` socket.error: [Errno 48] Address already in use ``` This fix manually creates a `TCPServer` with `allow_reuse_address` set to `True`, which solves this issue. Tested on OS X 10.9.1. --- pelican/tools/templates/fabfile.py.in | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pelican/tools/templates/fabfile.py.in b/pelican/tools/templates/fabfile.py.in index 73c64ac8..fb56ae85 100644 --- a/pelican/tools/templates/fabfile.py.in +++ b/pelican/tools/templates/fabfile.py.in @@ -1,6 +1,9 @@ from fabric.api import * import fabric.contrib.project as project import os +import sys +import SimpleHTTPServer +import SocketServer # Local path configuration (can be absolute or relative to fabfile) env.deploy_path = 'output' @@ -32,7 +35,16 @@ def regenerate(): local('pelican -r -s pelicanconf.py') def serve(): - local('cd {deploy_path} && python -m SimpleHTTPServer'.format(**env)) + os.chdir(env.deploy_path) + + PORT = 8000 + class AddressReuseTCPServer(SocketServer.TCPServer): + allow_reuse_address = True + + server = AddressReuseTCPServer(('', PORT), SimpleHTTPServer.SimpleHTTPRequestHandler) + + sys.stderr.write('Serving on port {0} ...\n'.format(PORT)) + server.serve_forever() def reserve(): build()