-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
191 lines (148 loc) · 5.79 KB
/
main.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
180
181
182
183
184
185
186
187
188
189
190
191
import os
from http.server import BaseHTTPRequestHandler, HTTPServer
DIRECTORY = "/web"
# author Cuneyt
# referenced http://aosabook.org/en/500L/a-simple-web-server.html
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
class case_no_file(object):
'''File or directory does not exist.'''
def test(self, handler):
return not os.path.exists(handler.full_path)
def act(self, handler):
raise Exception("'{0}' not found".format(handler.path))
class case_existing_file(object):
'''File exists.'''
def test(self, handler):
return os.path.isfile(handler.full_path)
def act(self, handler):
handler.handle_file(handler.full_path)
class case_always_fail(object):
'''Base case if nothing else worked.'''
def test(self, handler):
return True
def act(self, handler):
raise Exception("Unknown object '{0}'".format(handler.path))
class case_directory_index_file(object):
'''Serve index.html page for a directory.'''
def index_path(self, handler):
return os.path.join(handler.full_path, 'index.html')
def test(self, handler):
return os.path.isdir(handler.full_path) and os.path.isfile(self.index_path(handler))
def act(self, handler):
handler.handle_file(self.index_path(handler))
class case_directory_no_index_file(object):
'''Serve listing for a directory without an index.html page.'''
def index_path(self, handler):
return os.path.join(handler.full_path, 'index.html')
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 case_cgi_file(object):
'''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(BaseHTTPRequestHandler):
'''Handle HTTP requests by returning a fixed 'page'.'''
Cases = [case_no_file,
case_cgi_file,
case_existing_file,
case_directory_index_file,
case_directory_no_index_file,
case_always_fail]
Listing_Page = '''\
<html>
<body>
<ul>
{0}
</ul>
</body>
</html>
'''
def list_dir(self, full_path):
try:
entries = os.listdir(full_path)
bullets = ['<li>{0}</li>'.format(e)
for e in entries if not e.startswith('.')]
page = self.Listing_Page.format('\n'.join(bullets))
self.send_content(page.encode('utf-8'))
except OSError as msg:
msg = "'{0}' cannot be listed: {1}".format(self.path, msg)
self.handle_error(msg)
def do_GET(self):
try:
# Figure out what exactly is being requested.
self.full_path = os.getcwd() + DIRECTORY + self.path
# Figure out how to handle it.
for case in self.Cases:
handler = case()
if handler.test(self):
handler.act(self)
break
# Handle errors.
except Exception as msg:
print("error message", msg)
self.handle_error(msg)
def handle_file(self, full_path):
try:
with open(full_path, 'rb') as reader:
content = reader.read()
content = content.decode("utf-8")
content = self.create_page(content=content)
content = bytes(content, 'utf-8')
self.send_content(content)
except IOError as msg:
msg = "'{0}' cannot be read: {1}".format(self.path, msg)
self.send_error(msg)
def handle_error(self, msg):
error_page_dir = os.path.join(os.path.dirname(__file__), 'web/error.html')
try:
with open(error_page_dir, 'rb') as reader:
content = reader.read()
content = content.decode("utf-8")
content = content.format(path=self.path, msg=msg)
content = bytes(content, 'utf-8')
self.send_content(content, 404)
except IOError as msg:
msg = "'{0}' cannot be read: {1}".format(self.path, msg)
self.send_error(msg)
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)
def create_page(self, content):
values = {
'date_time': self.date_time_string(),
'client_host': self.client_address[0],
'client_port': self.client_address[1],
'command': self.command,
'path': self.path,
'full_path': self.full_path
}
page = content.format(**values)
return page
def send_page(self, page):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(len(page)))
self.end_headers()
self.wfile.write(page.encode('utf-8'))
def run_cgi(self, full_path):
cmd = "python " + full_path
child = os.popen(cmd, 'r', 1)
data = child.read()
child.close()
self.send_content(bytes(data, 'utf-8'))
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
serverAddress = ('localhost', 8080)
server = HTTPServer(serverAddress, RequestHandler)
server.serve_forever()