forked from drichard/mindmaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
229 lines (187 loc) · 6.15 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/python3
# Credits:
# https://www.acmesystems.it/python_http
# http://joelinoff.com/blog/?p=1658
import socket
from http.server import HTTPServer
# Credit: https://github.com/Granitosaurus/sauth
from sauth import SimpleHTTPAuthHandler
import time
import os
from urllib.parse import urlparse, parse_qs
import json
import glob
hostName = ""
hostPort = 8996
os.chdir("src")
SimpleHTTPAuthHandler.username = "santosh"
SimpleHTTPAuthHandler.password = "hello"
class MyServer(SimpleHTTPAuthHandler):
#Handler for the GET requests
def do_GET(self):
"""Present frontpage with user authentication."""
auth_header = self.headers.get('Authorization', '').encode('ascii')
if auth_header is None:
self.do_authhead()
self.wfile.write(b"no auth header received")
elif auth_header == self.valid_header:
path = urlparse(self.path)
query = path.query
allowed_filetypes = (".html", ".css", ".png", ".jpg", ".gif", ".ico",".js")
if self.path=="/":
self.path="/index.html"
self.do_index()
elif self.path == '/getmaps':
self.do_get_maps()
elif self.path == '/getfile':
self.do_get_file()
elif path.path.endswith(allowed_filetypes):
self.do_index()
else:
self.do_index()
else:
self.do_authhead()
self.wfile.write(auth_header)
self.wfile.write(b"not authenticated")
return None
def do_get_maps(self):
"""Present frontpage with user authentication."""
auth_header = self.headers.get('Authorization', '').encode('ascii')
if auth_header is None:
self.do_authhead()
self.wfile.write(b"no auth header received")
elif auth_header == self.valid_header:
print("Going to get maps")
files = glob.glob(os.getcwd() + "/maps/" + "*.json")
print(glob.glob(os.getcwd() + "/maps/" + "*.json"))
files = os.listdir(os.getcwd() + "/maps/");
self.send_response(200)
self.send_header('Content-type','text/json')
self.end_headers()
self.wfile.write(json.dumps(files).encode('utf-8'))
return
else:
self.do_authhead()
self.wfile.write(auth_header)
self.wfile.write(b"not authenticated")
def do_get_file(self):
"""Present frontpage with user authentication."""
auth_header = self.headers.get('Authorization', '').encode('ascii')
if auth_header is None:
self.do_authhead()
self.wfile.write(b"no auth header received")
elif auth_header == self.valid_header:
print("Going to open file")
files = glob.glob(os.getcwd() + "/maps/" + "*.json")
print(glob.glob(os.getcwd() + "/maps/" + "*.json"))
print(files[0])
self.send_response(200)
self.send_header('Content-type','text/json')
self.end_headers()
with open(files[0]) as data_file:
self.wfile.write(data_file.read().encode('utf-8'))
return
else:
self.do_authhead()
self.wfile.write(auth_header)
self.wfile.write(b"not authenticated")
# Credit: https://stackoverflow.com/questions/18346583/how-do-i-map-incoming-path-requests-when-using-httpserver
# https://stackoverflow.com/questions/3474045/problems-with-my-basehttpserver
# https://github.com/cuamckuu/NotesApp/blob/master/server.py
# https://github.com/davesnowdon/nao-wanderer/blob/master/wanderer/src/main/python/wanderer/http.py
# https://pymotw.com/3/http.server/index.html
def do_index(self):
try:
#Check the file extension required and
#set the right mime type
sendReply = False
if self.path.endswith(".html"):
mimetype='text/html'
sendReply = True
if self.path.endswith(".jpg"):
mimetype='image/jpg'
sendReply = True
if self.path.endswith(".gif"):
mimetype='image/gif'
sendReply = True
if self.path.endswith(".png"):
mimetype='image/png'
sendReply = True
if self.path.endswith(".js"):
mimetype='application/javascript'
sendReply = True
if self.path.endswith(".css"):
mimetype='text/css'
sendReply = True
if sendReply == True:
#Open the static file requested and send it
f = open(os.getcwd() + "/"+self.path, 'rb')
self.send_response(200)
self.send_header('Content-type',mimetype)
self.end_headers()
self.wfile.write(f.read())
f.close()
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
# POST is for submitting data.
def do_POST(self):
"""Present frontpage with user authentication."""
auth_header = self.headers.get('Authorization', '').encode('ascii')
if auth_header is None:
self.do_authhead()
self.wfile.write(b"no auth header received")
elif auth_header == self.valid_header:
if self.path=="/save":
self.do_save()
elif self.path == '/loadmap':
self.do_load_map()
else:
print( "incomming http: ", self.path )
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write(b"Ok!")
return
else:
self.do_authhead()
self.wfile.write(auth_header)
self.wfile.write(b"not authenticated")
def do_load_map(self):
print("Going to open the requested file")
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
data = json.loads(self.data_string)
# print(data)
print(data['title'])
# files = glob.glob(os.getcwd() + "/maps/" + "*.json")
# print(glob.glob(os.getcwd() + "/maps/" + "*.json"))
# print(files[0])
self.send_response(200)
self.send_header('Content-type','text/json')
self.end_headers()
with open(os.getcwd() + "/maps/" + data['title']) as data_file:
self.wfile.write(data_file.read().encode('utf-8'))
return
def do_save(self):
print("in post method")
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
self.send_response(200)
self.end_headers()
data = json.loads(self.data_string)
# print(data)
print(data['title'])
with open(os.curdir + "/maps/" + data['title']+".json", "w") as outfile:
json.dump(data, outfile)
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write(b"Ok!")
return
myServer = HTTPServer((hostName, hostPort), MyServer)
print(time.asctime(), "Server Starts - %s:%s" % (hostName, hostPort))
try:
myServer.serve_forever()
except KeyboardInterrupt:
pass
myServer.server_close()
print(time.asctime(), "Server Stops - %s:%s" % (hostName, hostPort))