1
1
mirror of https://github.com/rsms/inter.git synced 2024-12-14 09:04:10 +03:00
inter/docs/lab/serve.py

49 lines
1.2 KiB
Python
Raw Normal View History

2018-09-06 04:45:10 +03:00
#!/usr/bin/env python
from __future__ import print_function, absolute_import
import os, sys
import signal
import socket
2019-01-28 06:19:56 +03:00
import http.server
from os.path import dirname, abspath, join as pjoin
2018-09-06 04:45:10 +03:00
def sighandler(signum, frame):
sys.stdout.write('\n')
sys.stdout.flush()
sys.exit(1)
2019-01-28 06:19:56 +03:00
class HTTPServer(http.server.HTTPServer):
def __init__(self, addr):
http.server.HTTPServer.__init__(
self, addr, http.server.SimpleHTTPRequestHandler)
2018-09-06 04:45:10 +03:00
def server_bind(self):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
2019-01-28 06:19:56 +03:00
http.server.HTTPServer.server_bind(self)
2018-09-06 04:45:10 +03:00
2019-01-28 06:19:56 +03:00
# make sure "./fonts" is a symlink to /build/fonts
labdir = abspath(dirname(__file__))
try:
os.symlink('../../build/fonts', pjoin(labdir, 'fonts'))
except OSError:
pass
addr = ("localhost", 3003)
2018-09-06 04:45:10 +03:00
2019-04-01 05:25:31 +03:00
if len(sys.argv) > 1:
if sys.argv[1] == '-h':
print('usage: %s [-h | --bind-any]' % sys.argv[0], file=sys.stdout)
sys.exit(0)
elif sys.argv[1] == '--bind-any':
addr = ("0.0.0.0", 3003)
2018-09-06 04:45:10 +03:00
# make ^C instantly exit program
signal.signal(signal.SIGINT, sighandler)
2019-01-28 06:19:56 +03:00
os.chdir(os.path.dirname(os.path.abspath(__file__)))
2018-09-06 04:45:10 +03:00
2019-01-28 06:19:56 +03:00
httpd = HTTPServer(addr)
print("serving at http://%s:%d/" % addr)
2018-09-06 04:45:10 +03:00
httpd.serve_forever()