-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
49 lines (33 loc) · 1.03 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
from tornado.web import Application, RequestHandler
from tornado.ioloop import IOLoop
import json
import dateutil.parser
listado = []
fields = ['tipo', 'fecha','volumen']
######## API #############
class AddData(RequestHandler):
def post(self):
json_dict = json.loads(self.request.body)
validation = all(elem in json_dict.keys() for elem in fields)
if validation:
json_dict['fecha'] = dateutil.parser.parse(json_dict['fecha'])
listado.append(json_dict)
self.write({'response' : 'OK'})
else:
self.write({'response' : 'ERROR'})
class History(RequestHandler):
def get(self):
json = json.dumps(listado)
self.write(json)
############## MAIN ##########
def main():
urls = [
("/api/data", AddData),
("/api/history", History),
]
return Application(urls, debug = True)
#################################
if __name__ == '__main__':
app = main()
app.listen(8080)
IOLoop.instance().start()