I have a rudimentary web server running next to a dev server. This simple web server listens for a request (on a different port) and touches the project's manage.py. This in turn triggers a restart:
reloader.py
#!/usr/bin/env python
import os
import logging
import SocketServer
from BaseHTTPServer import BaseHTTPRequestHandler
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p')
PORT = 9001
FILE_TO_TOUCH = '/path/to/myenv/manage.py'
def touch(fname, times=None):
with open(fname, 'a'):
os.utime(fname, times)
class HandleTouch(BaseHTTPRequestHandler):
def do_GET(self):
logging.info("Touching %s", FILE_TO_TOUCH)
touch(FILE_TO_TOUCH)
self.send_response(200)
self.send_header("Content-Length", '0')
self.end_headers()
httpd = SocketServer.TCPServer(("", PORT), HandleTouch)
logging.info("Server listening on port %s", PORT)
httpd.serve_forever()
On the host machine I use a simple shell command to watch for file changes and hit the reloader's endpoint:
watcher.sh
#!/bin/sh
watchmedo shell-command -c 'curl http://192.168.10.10:9001' -R -i '/path/to/myenv/manage.py' -p '*.py' ./
Where watchmedo
is a utility from watchdog.