Skip to content

Instantly share code, notes, and snippets.

@hostage
Created February 21, 2012 06:25
Show Gist options
  • Save hostage/1874226 to your computer and use it in GitHub Desktop.
Save hostage/1874226 to your computer and use it in GitHub Desktop.
Simple python snippet to serve files via http from the current folder. Automatically opens a browser pointed at the root url as a convenience.
# Launch an http server on localhost using a random port to serve files from
# the current working folder. Automatically open a web browser pointed at the
# root url as a convenience.
from SimpleHTTPServer import SimpleHTTPRequestHandler
from BaseHTTPServer import HTTPServer
import webbrowser
# Create a localhost server, specify port 0 so it will pick one for us.
httpd = HTTPServer(("localhost", 0), SimpleHTTPRequestHandler)
print "Server started on port: " + str(httpd.server_port)
# Open the browser; if index.html exists it will be opened, otherwise the user
# will get a directory listing.
webbrowser.open_new_tab("http://localhost:" + str(httpd.server_port))
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment