Skip to content

Instantly share code, notes, and snippets.

@juancarlospaco
Created September 24, 2015 05:54
Show Gist options
  • Save juancarlospaco/6ea50425aa025e1a7160 to your computer and use it in GitHub Desktop.
Save juancarlospaco/6ea50425aa025e1a7160 to your computer and use it in GitHub Desktop.
HTTP Server with LiveReload if any else builtin Simple HTTP Server, asks to install LiveReload if not installed but still works Ok, tested on Python 3.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""HTTP Serve with LiveReload."""
import os
from webbrowser import open_new_tab
try: # https://github.com/lepture/python-livereload
import livereload # sudo pip3 install --upgrade livereload
except ImportError:
from webbrowser import open_new_tab
import http.server as server
from http.server import CGIHTTPRequestHandler
livereload = None # Still works Ok without LiveReload
def serve_http(where=os.getcwd()):
"""Serve HTTP files and HTML on the where folder,with LiveReload if any."""
if livereload: # with LiveReload
livereload.Server().serve(port=8000, host="0.0.0.0",
open_url_delay=1, root=where)
else: # without LiveReload
print("Run:pip install livereload\nServer on http://localhost:8000")
httpd = server.HTTPServer(('', 8000), CGIHTTPRequestHandler)
open_new_tab("http://localhost:8000/")
httpd.serve_forever()
if __name__ in '__main__':
serve_http()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment