Adding ability to listen on addresses other than localhost.

This is helpful for mobile testing of Pelican sites by allowing
broadcasting on the local network. Using port 80 requires
running as root on most machines.
This commit is contained in:
Danny Hermes 2015-01-10 15:40:54 -08:00
commit 3d8ceb1c67
2 changed files with 35 additions and 25 deletions

View file

@ -12,7 +12,8 @@ try:
except ImportError: except ImportError:
import socketserver # NOQA import socketserver # NOQA
PORT = len(sys.argv) == 2 and int(sys.argv[1]) or 8000 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'] SUFFIXES = ['', '.html', '/index.html']
@ -38,13 +39,13 @@ Handler = ComplexHTTPRequestHandler
socketserver.TCPServer.allow_reuse_address = True socketserver.TCPServer.allow_reuse_address = True
try: try:
httpd = socketserver.TCPServer(("", PORT), Handler) httpd = socketserver.TCPServer((SERVER, PORT), Handler)
except OSError as e: except OSError as e:
logging.error("Could not listen on port %s", PORT) logging.error("Could not listen on port %s, server %s", PORT, SERVER)
sys.exit(getattr(e, 'exitcode', 1)) sys.exit(getattr(e, 'exitcode', 1))
logging.info("Serving at port %s", PORT) logging.info("Serving at port %s, server %s", PORT, SERVER)
try: try:
httpd.serve_forever() httpd.serve_forever()
except KeyboardInterrupt as e: except KeyboardInterrupt as e:

View file

@ -32,26 +32,27 @@ ifeq ($(DEBUG), 1)
endif endif
help: help:
@echo 'Makefile for a pelican Web site ' @echo 'Makefile for a pelican Web site '
@echo ' ' @echo ' '
@echo 'Usage: ' @echo 'Usage: '
@echo ' make html (re)generate the web site ' @echo ' make html (re)generate the web site '
@echo ' make clean remove the generated files ' @echo ' make clean remove the generated files '
@echo ' make regenerate regenerate files upon modification ' @echo ' make regenerate regenerate files upon modification '
@echo ' make publish generate using production settings ' @echo ' make publish generate using production settings '
@echo ' make serve [PORT=8000] serve site at http://localhost:8000' @echo ' make serve [PORT=8000] serve site at http://localhost:8000'
@echo ' make devserver [PORT=8000] start/restart develop_server.sh ' @echo ' make serve-global [SERVER=0.0.0.0] serve (as root) to $(SERVER):80 '
@echo ' make stopserver stop local server ' @echo ' make devserver [PORT=8000] start/restart develop_server.sh '
@echo ' make ssh_upload upload the web site via SSH ' @echo ' make stopserver stop local server '
@echo ' make rsync_upload upload the web site via rsync+ssh ' @echo ' make ssh_upload upload the web site via SSH '
@echo ' make dropbox_upload upload the web site via Dropbox ' @echo ' make rsync_upload upload the web site via rsync+ssh '
@echo ' make ftp_upload upload the web site via FTP ' @echo ' make dropbox_upload upload the web site via Dropbox '
@echo ' make s3_upload upload the web site via S3 ' @echo ' make ftp_upload upload the web site via FTP '
@echo ' make cf_upload upload the web site via Cloud Files' @echo ' make s3_upload upload the web site via S3 '
@echo ' make github upload the web site via gh-pages ' @echo ' make cf_upload upload the web site via Cloud Files'
@echo ' ' @echo ' make github upload the web site via gh-pages '
@echo 'Set the DEBUG variable to 1 to enable debugging, e.g. make DEBUG=1 html' @echo ' '
@echo ' ' @echo 'Set the DEBUG variable to 1 to enable debugging, e.g. make DEBUG=1 html '
@echo ' '
html: html:
$$(PELICAN) $$(INPUTDIR) -o $$(OUTPUTDIR) -s $$(CONFFILE) $$(PELICANOPTS) $$(PELICAN) $$(INPUTDIR) -o $$(OUTPUTDIR) -s $$(CONFFILE) $$(PELICANOPTS)
@ -69,6 +70,14 @@ else
cd $$(OUTPUTDIR) && $(PY) -m pelican.server cd $$(OUTPUTDIR) && $(PY) -m pelican.server
endif endif
serve-global:
ifdef SERVER
cd $$(OUTPUTDIR) && $(PY) -m pelican.server 80 $$(SERVER)
else
cd $$(OUTPUTDIR) && $(PY) -m pelican.server 80 0.0.0.0
endif
devserver: devserver:
ifdef PORT ifdef PORT
$$(BASEDIR)/develop_server.sh restart $$(PORT) $$(BASEDIR)/develop_server.sh restart $$(PORT)
@ -106,4 +115,4 @@ github: publish
ghp-import -m "Generate Pelican site" -b $(GITHUB_PAGES_BRANCH) $$(OUTPUTDIR) ghp-import -m "Generate Pelican site" -b $(GITHUB_PAGES_BRANCH) $$(OUTPUTDIR)
git push origin $(GITHUB_PAGES_BRANCH) git push origin $(GITHUB_PAGES_BRANCH)
.PHONY: html help clean regenerate serve devserver publish ssh_upload rsync_upload dropbox_upload ftp_upload s3_upload cf_upload github .PHONY: html help clean regenerate serve serve-global devserver publish ssh_upload rsync_upload dropbox_upload ftp_upload s3_upload cf_upload github