-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic_cgi_index_listing_server.py
179 lines (144 loc) · 5.08 KB
/
static_cgi_index_listing_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import http.server
import os
import subprocess
# I think this is something that was defined in Python2, but was not in Python3
class ServerException(Exception):
pass
class BaseCase(object):
def handle_file(self, handler, full_path):
try:
with open(full_path, 'rb') as reader:
content = reader.read()
handler.send_content(content)
except IOError as msg:
msg = "'{0}' cannot be read: {1}".format(full_path, msg)
handler.handle_error(msg)
def index_path(self, handler):
return os.path.join(handler.full_path, 'index.html')
def test(self, handler):
assert False, 'Not implemented.'
def act(self, handler):
assert False, 'Not implemented.'
class CaseNoFile(BaseCase):
'''File or directory does not exist.'''
def test(self, handler):
return not os.path.exists(handler.full_path)
def act(self, handler):
raise ServerException("'{0}' not found".format(handler.path))
class CaseExistingFile(BaseCase):
'''File exists.'''
def test(self, handler):
return os.path.isfile(handler.full_path)
def act(self, handler):
self.handle_file(handler, handler.full_path)
class CaseAlwaysFail(BaseCase):
'''Base case if nothing else worked.'''
def test(self, handler):
return True
def act(self, handler):
raise ServerException("Unknown object '{0}'".format(handler.path))
class CaseDirectoryIndexFile(BaseCase):
'''Serve index.html page for a directory.'''
def test(self, handler):
return os.path.isdir(handler.full_path) and \
os.path.isfile(self.index_path(handler))
def act(self, handler):
self.handle_file(handler, self.index_path(handler))
class CaseDirectoryNoIndexFile(BaseCase):
'''Serve listing for a directory without an index.html page.'''
def test(self, handler):
return os.path.isdir(handler.full_path) and \
not os.path.isfile(self.index_path(handler))
def act(self, handler):
handler.list_dir(handler.full_path)
class CaseCgiFile(BaseCase):
'''Something runnable.'''
def test(self, handler):
return os.path.isfile(handler.full_path) and handler.full_path.endswith('.py')
def act(self, handler):
handler.run_cgi(handler.full_path)
class RequestHandler(http.server.BaseHTTPRequestHandler):
'''Handle HTTP requests by returning a fixed 'page'.'''
# ...page template...
Page = '''\
<html>
<body>
<table>
<tr> <td>Header</td> <td>Value</td> </tr>
<tr> <td>Date and time</td> <td>{date_time}</td> </tr>
<tr> <td>Client host</td> <td>{client_host}</td> </tr>
<tr> <td>Client port</td> <td>{client_port}</td> </tr>
<tr> <td>Command</td> <td>{command}</td> </tr>
<tr> <td>Path</td> <td>{path}</td> </tr>
</table>
</body>
</html>
'''
Listing_Page = '''\
<html>
<body>
<ul>
{0}
</ul>
</body>
</html>
'''
Error_Page = '''\
<html>
<body>
<h1>Error accessing {path}</h1>
<p>{msg}</p>
</body>
</html>
'''
'''
If the requested path maps to a file, that file is served.
If anything goes wrong, an error page is constructed.
'''
Cases = [CaseNoFile(),
CaseCgiFile(),
CaseExistingFile(),
CaseDirectoryIndexFile(),
CaseDirectoryNoIndexFile(),
CaseAlwaysFail()]
def do_GET(self):
try:
# Figure out what exactly is being requested.
# self.path = javascript.html here
self.full_path = os.getcwd() + self.path
for case in self.Cases:
handler = case
if handler.test(self):
handler.act(self)
break
# Handle errors.
except Exception as msg:
self.handle_error(msg)
def list_dir(self, full_path):
try:
entries = os.listdir(full_path)
bullets = ['<li><h1>{0}</h1></li>'.format(e)
for e in entries if not e.startswith('.')]
content = str.encode(self.Listing_Page.format('\n'.join(bullets)))
self.send_content(content)
except OSError as msg:
msg = "'{0}' cannot be listed: {1}".format(self.path, msg)
self.handle_error(msg)
def run_cgi(self, full_path):
# check_output will run the subprocess and return stdout
data = subprocess.check_output(["python3", full_path])
self.send_content(data)
def handle_error(self, msg):
content = str.encode(self.Error_Page.format(path=self.path, msg=msg))
self.send_content(content, 404)
def send_content(self, content, status=200):
self.send_response(status)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(len(content)))
self.end_headers()
self.wfile.write(content)
#----------------------------------------------------------------------
if __name__ == '__main__':
serverAddress = ('', 8080)
server = http.server.HTTPServer(serverAddress, RequestHandler)
server.serve_forever()