-
Notifications
You must be signed in to change notification settings - Fork 2
/
server_backend_test.py
44 lines (33 loc) · 1.17 KB
/
server_backend_test.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
import os
from tornado.ncss import Server, ncssbook_log
TEMPLATE_DIR = 'templates'
def render_file(response, filename:str, variables) -> None:
"""Renders the filename replaceing {name} with keys in variables"""
response.write(get_template(filename).format(**variables))
def get_template(filename):
"""
Gets the template from TEMPLATE_DIR with name filename
"""
with open(os.path.join(TEMPLATE_DIR, filename)) as f:
return f.read()
def index_handler(request):
request.write("hello there")
param_name = request.get_field("name")
if param_name != None:
request.write("<br>")
request.write(param_name)
request.write("!")
def book_handler(request, book_id):
request.write(book_id)
def test_reader(request):
template = get_template('test.html')
fname = request.get_field('fname')
lname = request.get_field('lname')
food = request.get_field('food')
user = {'fname': fname, 'lname': lname, 'food': food}
render_file(request, 'test.html', user)
server = Server()
server.register(r'/', index_handler)
server.register(r'/book/(\d+)/?', book_handler)
server.register(r'/test', test_reader)
server.run()