-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSimpleServer.py
119 lines (86 loc) · 3.18 KB
/
SimpleServer.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
try:
from http.server import BaseHTTPRequestHandler, HTTPServer
except:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from time import sleep
import os, sys
import inspect
PORT_NUMBER = int(sys.argv[1])
dirpath = os.path.dirname(os.path.realpath(sys.argv[0]))
assets = dirpath + '/../'
www = dirpath + '/www/'
class ServerHandler(BaseHTTPRequestHandler):
legacy = sys.version_info < (3, 0)
method = "GET"
def do_GET(self):
self.doRequestHttp()
def do_PUT(self):
self.method = "PUT"
self.doRequestHttp()
def do_HEAD(self):
self.method = "HEAD"
self.doRequestHttp()
def do_POST(self):
self.method = "POST"
self.doRequestHttp()
def getInput(self):
contentLength = self.headers['content-length'] if 'content-length' in self.headers else '0'
if contentLength:
contentLength = int(contentLength)
return self.rfile.read(contentLength)
return '' if self.legacy else bytes('')
def displayInput(self, contents):
data = self.getInput()
param = '{FORMDATA}'
pattern = '<'
replacement = '<'
alternative = ''
if not self.legacy:
param = bytes(param, 'utf-8')
pattern = bytes(pattern, 'utf-8')
replacement = bytes(replacement, 'utf-8')
alternative = bytes(alternative, 'utf-8')
if data:
return contents.replace(param, data.replace(pattern, replacement))
else:
return alternative
def doRequestHttp(self):
if self.path == '/':
self.path = '/index.html'
if self.path.endswith('.html'):
contentType = 'text/html; charset=UTF-8'
elif self.path.endswith('.txt'):
contentType = 'text/plain; charset=UTF-8'
elif self.path.endswith('.json'):
contentType = 'application/json; charset=UTF-8'
elif self.path.endswith('.js'):
contentType = 'application/javascript'
elif self.path.endswith('.css'):
contentType = 'text/css'
else:
contentType = 'application/octet-stream'
if self.path.startswith('/assets/'):
current = assets + self.path[8:]
else:
current = www + self.path
if 'x-pjax' in self.headers and self.headers['x-pjax'] == 'true':
sleep(2) # Simulate slow page
try:
with open(current, 'rb') as f:
self.send_response(200)
self.send_header('content-type', contentType)
self.end_headers()
fileContents = f.read()
if self.method == 'POST' and fileContents:
fileContents = self.displayInput(fileContents)
self.wfile.write(fileContents)
f.close()
except IOError:
self.send_error(404, 'File not found: %s' % current)
try:
server = HTTPServer(('', PORT_NUMBER), ServerHandler)
print('Server is listening on port ' + str(PORT_NUMBER))
server.serve_forever()
except KeyboardInterrupt:
print('^C received, shutting down the web server')
server.socket.close()