HTTP Watcher (Python Library)

0.5.2 · active · verified Thu Apr 16

httpwatcher is a Python library and command-line utility for serving static files with live reload functionality, primarily intended for web development. It leverages the Tornado asynchronous web framework for its HTTP and WebSocket server capabilities. The current version is 0.5.2. Releases appear to be ad-hoc, tied to development needs rather than a fixed cadence.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up and run an `httpwatcher` server programmatically. It serves files from a 'my_static_app' directory, includes a basic `index.html` file, and starts the server on `http://127.0.0.1:5555`. Changes to files in 'my_static_app' would trigger a live reload in connected browsers. The `open_browser=False` argument is used for programmatic control.

import os
from httpwatcher import HttpWatcherServer
from tornado.ioloop import IOLoop

# Create a dummy directory and file for demonstration
if not os.path.exists('my_static_app'):
    os.makedirs('my_static_app')
with open('my_static_app/index.html', 'w') as f:
    f.write('<!DOCTYPE html><html><head><title>Test</title></head><body><h1>Hello, httpwatcher!</h1></body></html>')

# Instantiate the server
server = HttpWatcherServer(
    root='my_static_app',
    host='127.0.0.1',
    port=5555,
    base_path='/',
    open_browser=False,
    verbose=True
)

# Start the server (usually in a separate thread or process for non-blocking operations)
# For a quick runnable example, we'll run it directly, which will block.
# In a real application, you might use IOLoop.current().start() in a thread.
print(f"Serving 'my_static_app' at http://127.0.0.1:5555")
print("Press Ctrl+C to stop.")

try:
    server.start() # This blocks the current thread
except KeyboardInterrupt:
    print("Server stopped.")
finally:
    server.stop()
    # Clean up dummy files/directories
    os.remove('my_static_app/index.html')
    os.rmdir('my_static_app')

view raw JSON →