-
Notifications
You must be signed in to change notification settings - Fork 43
/
start_python_server.py
72 lines (57 loc) · 2.17 KB
/
start_python_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
#!/usr/bin/env pyhthon
'''
Starts a very basic python-based webserver for debugging.
It's very important that this server be started in the main music21j directory, not in just any
directory, so call
cd ~/git/music21j
python server/start_python_server.py
'''
import sys
try:
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
from CGIHTTPServer import _url_collapse_path
except ImportError: # python 3
from http.server import HTTPServer
from http.server import CGIHTTPRequestHandler
from http.server import _url_collapse_path
try:
import cgitb;
cgitb.enable() # Error reporting
except ImportError:
pass # deprecated in Python 3.11 to be removed in 3.13. Not necessary.
class MykeCGIHTTPServer(CGIHTTPRequestHandler):
def is_cgi(self):
"""
Test whether self.path corresponds to a CGI script.
Returns True and updates the cgi_info attribute to the tuple
(dir, rest) if self.path requires running a CGI script.
Returns False otherwise.
If any exception is raised, the caller should assume that
self.path was rejected as invalid and act accordingly.
The default implementation tests whether the normalized url
path begins with one of the strings in self.cgi_directories
(and the next character is a '/' or the end of the string).
"""
collapsed_path = _url_collapse_path(self.path)
for path in self.cgi_directories:
if path in collapsed_path:
dir_sep_index = collapsed_path.rfind(path) + len(path)
head, tail = collapsed_path[:dir_sep_index], collapsed_path[dir_sep_index + 1:]
self.cgi_info = head, tail
return True
return False
def main():
server = HTTPServer
handler = MykeCGIHTTPServer
# handler.cgi_directories.append("/server/cgi-bin/")
if len(sys.argv) > 1:
port = int(sys.argv[1])
else:
port = 8000
server_address = ("", port)
httpd = server(server_address, handler)
print("Beginning HTTP/CGI server at " + str(port));
httpd.serve_forever()
if __name__ == '__main__':
main()