-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
56 lines (51 loc) · 2.01 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
from http.server import BaseHTTPRequestHandler, HTTPServer
from os import path
from titlegen import TitleGenerator
import json, io, sys
WEB_ROOT = 'static'
TGEN = None
class TheGarlicServer(BaseHTTPRequestHandler):
mimeTypes = {
'txt': 'text/plain',
'html': 'text/html',
'css': 'text/css',
'js': 'application/javascript',
'png': 'image/png',
'jpg': 'image/jpeg',
'ico': 'image/x-icon'
}
def do_GET(self):
content = ''.encode('utf-8')
if self.path.startswith('/api/post.json'):
titles = dict(titles=[TGEN.generateTitle() for _ in range(5)])
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.send_header('Cache-Control', 'no-cache')
content = json.dumps(titles).encode('utf-8')
else:
filepath = self.path.split('?')[0]
if filepath.endswith('/'):
filepath += 'index.html'
filepath = WEB_ROOT + path.join(*('.' + filepath).split('/'))[1:]
try:
ext = filepath.split('.')[-1]
with io.open(filepath, 'rb') as f:
self.send_response(200)
self.send_header('Content-Type', self.mimeTypes.get(ext, 'application/octet-stream'))
self.send_header('Cache-Control', 'max-age=1800')
content = f.read()
except Exception as e:
self.send_response(404)
self.send_header('Content-Type', 'text/plain')
content = ('404 ' + self.path).encode('utf-8')
self.send_header('Content-Length', str(len(content)))
self.end_headers()
self.wfile.write(content)
if __name__ == '__main__':
TGEN = TitleGenerator(150)
server_address = ('127.0.0.1', 9420)
if len(sys.argv) > 1:
server_address = ('', int(sys.argv[1]))
httpd = HTTPServer(server_address, TheGarlicServer)
print('running server...')
httpd.serve_forever()