Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | import SimpleHTTPServer |
| 2 | import multiprocessing |
| 3 | import os |
| 4 | |
| 5 | class HTTPServer(SimpleHTTPServer.BaseHTTPServer.HTTPServer): |
| 6 | |
| 7 | def server_start(self, root_dir): |
| 8 | import signal |
| 9 | signal.signal(signal.SIGTERM, signal.SIG_DFL) |
| 10 | os.chdir(root_dir) |
| 11 | self.serve_forever() |
| 12 | |
| 13 | class HTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): |
| 14 | |
| 15 | def log_message(self, format_str, *args): |
| 16 | pass |
| 17 | |
| 18 | class HTTPService(object): |
| 19 | |
| 20 | def __init__(self, root_dir, host=''): |
| 21 | self.root_dir = root_dir |
| 22 | self.host = host |
| 23 | self.port = 0 |
| 24 | |
| 25 | def start(self): |
| 26 | self.server = HTTPServer((self.host, self.port), HTTPRequestHandler) |
| 27 | if self.port == 0: |
| 28 | self.port = self.server.server_port |
| 29 | self.process = multiprocessing.Process(target=self.server.server_start, args=[self.root_dir]) |
| 30 | self.process.start() |
| 31 | |
| 32 | def stop(self): |
| 33 | self.server.server_close() |
| 34 | self.process.terminate() |
| 35 | self.process.join() |