forked from calebsmith/gdi-intro-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·78 lines (57 loc) · 1.76 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
"""
A server that works with the livereload chrome extension to automatically
refresh the browser whenver a set of files changes. The server degrades to a
SimpleHTTPServer w/o livereload capability if the livereload Python package is
not available.
The default port is 8000. Supply a commandline argument for a different port.
This serves files from the current directory on your local browser and is good
for viewing slides easily.
E.g. Links to slides for all classes can viewed by visiting:
http://localhost:8000/
on the same computer that is running the server.
Usage:
python server.py
or
python server.py <port>
"""
import sys
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
HandlerClass = SimpleHTTPRequestHandler
ServerClass = BaseHTTPServer.HTTPServer
Protocol = "HTTP/1.0"
SERVER_ADDRESS = '127.0.0.1'
try:
from livereload import Server as LiveServer
except ImportError:
LiveServer = None
DEFAULT_PORT = 8000
patterns = [
'*.html',
'set*/slides/*.md',
'set*/*.html',
'images/',
'css/',
]
def main(port):
if LiveServer is not None:
live_reload_server(port)
else:
print("Using SimpleHTTPServer. Follow the README for livereload if "
"desired")
simple_server(port)
def simple_server(port):
HandlerClass.protocol_version = Protocol
httpd = ServerClass((SERVER_ADDRESS, port), HandlerClass)
sa = httpd.socket.getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
httpd.serve_forever()
def live_reload_server(port):
server = LiveServer()
for pattern in patterns:
server.watch(pattern)
server.serve(port=port)
if __name__ == '__main__':
port = int(sys.argv[1]) if sys.argv[1:] else 8000
main(port)