Skip to content

Instantly share code, notes, and snippets.

@airtonix
Last active December 17, 2015 05:59
Show Gist options
  • Save airtonix/5562266 to your computer and use it in GitHub Desktop.
Save airtonix/5562266 to your computer and use it in GitHub Desktop.
improved simple python local webserver.
#!/usr/bin/env python
"""
Installation
------------
mkdir -p ~/bin
wget https://gist.github.com/airtonix/5562266 -o ~/bin/webserver
chmod +x ~/bin/webserver
Usage
-----
# answers on port 9000 on any ip address (good for .local)
webserver 0:9000
# only answers on port 8000 of 192.168.1.10 or hostnames pointing at 192.168.1.10
webserver 192.168.1.10:8000
"""
import sys
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
HandlerClass = SimpleHTTPRequestHandler
ServerClass = BaseHTTPServer.HTTPServer
Protocol = "HTTP/1.0"
args = sys.argv[1:]
host = '127.0.0.1'
port = 8000
if len(args) > 0:
address = args[0]
if ":" in address:
host, port = address.split(":")
if host is None:
host = 0
else:
port = address
server_address = (host, int(port))
HandlerClass.protocol_version = Protocol
httpd = ServerClass(server_address, HandlerClass)
sa = httpd.socket.getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment