-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·83 lines (75 loc) · 2.77 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
#!/usr/bin/env python
#coding: utf-8
"""
Serves files out of its current directory.
Doesn't handle POST requests.
"""
import SocketServer
import SimpleHTTPServer
from urlparse import urlparse, parse_qs
import os
ADDRESS = '' #any
PORT = 8080
STORAGEDIR = 'userfiles'
def getfile():
"""returns a file to the client"""
return 'getfile'
class CustomHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
data = urlparse(self.path)
if data.path == '/listfiles':
#list user source code files stored on the server
files = '\n'.join(os.listdir(STORAGEDIR))
self.send_response(200) #OK
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(files)
return
elif data.path == '/getfile':
#sends the sourcecode file to the client
self.send_response(200) #OK
self.send_header('Content-type', 'text/html')
self.end_headers()
query = parse_qs(data.query)
filename = query['name'][0]
if not filename.endswith('.splaf'):
filename += '.splaf'
try:
file = open(STORAGEDIR + os.sep + filename, 'r')
filedata = file.read()
file.close()
except IOError:
self.wfile.write('Error! Could not get file %s!' % filename)
else:
self.wfile.write(filedata)
return
elif data.path == '/submitfile':
#gets the source code file from the client
self.send_response(200) #OK
self.send_header('Content-type', 'text/html')
self.end_headers()
query = parse_qs(data.query)
filename = query['name'][0]
filedata = query['data'][0]
if not filename.endswith('.splaf'):
filename += '.splaf'
try:
file = open(STORAGEDIR + os.sep + filename, 'w')
file.write(filedata)
file.close()
except IOError:
self.wfile.write('There was an error while trying to store '+\
'file %s on the server!' % filename)
else:
self.wfile.write('File %s correctly stored on the server.' %
filename)
return
elif data.path.endswith(('.html', '.js', '.css', '.ico')):
#serve files by following self.path from current working directory
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
return
else:
self.send_error(404) #not found
server = SocketServer.ThreadingTCPServer((ADDRESS, PORT), CustomHandler)
print "Serving at port", PORT
server.serve_forever()