Skip to content

Instantly share code, notes, and snippets.

@harrislapiroff
Created May 26, 2011 15:15
Show Gist options
  • Save harrislapiroff/993343 to your computer and use it in GitHub Desktop.
Save harrislapiroff/993343 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import os
import SimpleHTTPServer
import BaseHTTPServer
from optparse import OptionParser, make_option as opt
# these are the defaults for server settings
DEFAULT_PORT = 8000
DEFAULT_ADDRESS = 'localhost'
DEFAULT_DIRECTORY = os.getcwd() # current directory
# define the options this script takes
option_list = [
opt('-d', '--directory', help='serve this directory [default: working directory]', type='string', default=DEFAULT_DIRECTORY),
]
parser = OptionParser(option_list=option_list, add_help_option=True, usage='Usage: hype.py [address[:port]] [options]')
(options, args) = parser.parse_args()
# parse out those options into these variables
directory = options.directory
address = DEFAULT_ADDRESS
port = DEFAULT_PORT
if len(args) > 0:
bits = args[0].split(':')
address = bits[0]
port = int(bits[1]) if len(bits) > 1 else DEFAULT_PORT
# replace ~ with absolute path of the user directory
if directory[0] is '~':
directory = os.environ['HOME'] + directory[1:]
# check for valid port
# make sure we can get to the user directory
try:
os.chdir(directory)
except OSError:
parser.error("Unable to use directory `%s'" % directory)
# we'll use the basic requesthandler that comes with python
handler = SimpleHTTPServer.SimpleHTTPRequestHandler
server_address = (address, port)
server = BaseHTTPServer.HTTPServer(server_address, handler)
print "Hyper is serving %s at http://%s:%d/\nQuit the server with CONTROL-C." % (directory, address, port)
try:
server.serve_forever()
except KeyboardInterrupt:
print '\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment