-
Notifications
You must be signed in to change notification settings - Fork 1
/
cr-test-server.py
executable file
·94 lines (78 loc) · 2.43 KB
/
cr-test-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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python
import BaseHTTPServer
import getopt
import os
import socket
import sys
DEFAULT_HTTP_PORT = 8000
INDEX_PAGE = "play.html"
class CRTestServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
response = 200
gzip = False
contentType = None
page = None
# remove leading '/'
path = self.path[1:]
if path == '':
# serve up the base static page
contentType = "text/html"
page = INDEX_PAGE
elif path.endswith(".js"):
contentType = "application/javascript"
page = path
elif path.endswith(".jsgz"):
contentType = "application/javascript"
gzip = True
page = path
elif os.path.exists(path):
contextType = "application/octet-stream"
page = path
else:
response = 404
self.send_response(response)
if response == 200:
if gzip:
self.send_header("Content-encoding", "gzip")
self.send_header("Content-type", contentType)
self.end_headers()
self.wfile.write(open(page).read())
else:
self.end_headers()
def run(port,
server_class=BaseHTTPServer.HTTPServer,
handler_class=CRTestServerHandler):
server_address = ('', port)
try:
httpd = server_class(server_address, handler_class)
except socket.error:
print "port %d is already is use" % (port)
return
print "Cirrus Retro test server running on port %d... (Ctrl-C to exit)" % (port)
httpd.serve_forever()
def usage():
print """USAGE: ghettorss-server.py <options>
-h, --help: Print this message
-p, --port=[number]: Port on which to run the HTTP server
"""
if __name__ == '__main__':
port = DEFAULT_HTTP_PORT
# process command line arguments
opts, args = getopt.getopt(sys.argv[1:], "hp:", ["help", "port="])
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-p", "--port"):
try:
port = int(arg)
except ValueError:
print "Invalid port number"
sys.exit()
print """
*** WARNING! Listening on all interfaces!
*** This will expose parts of your filesystem to anyone who cares to
*** access it with a web browser!
"""
# kick off the server
run(port)