-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
63 lines (43 loc) · 1.43 KB
/
client.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
from tornado.web import Application, RequestHandler
from tornado.ioloop import IOLoop
from tornado.httpclient import AsyncHTTPClient, HTTPClient
from tornado import httpclient
import json
import datetime
SERVER_URL = 'http://localhost:8080'
ADD_DATA_URL = SERVER_URL + '/api/data'
HISTORY_URL = SERVER_URL + '/api/history'
######## Request #############
class AddData(RequestHandler):
def post(self):
'''
introduce la fecha y envia a el servidor
'''
data = self.request.body
data_dict = json.loads(data)
date_now = datetime.datetime.now().isoformat()
data_dict['fecha'] = date_now
data = json.dumps(data_dict)
http_client = AsyncHTTPClient()
post_req = httpclient.HTTPRequest(ADD_DATA_URL,
body = data,
method="POST")
response = http_client.fetch(post_req)
self.write({'respuesta' : 'OK'})
class History(RequestHandler):
def get(self):
http_client = AsyncHTTPClient()
resp = http_client.fetch(HISTORY_URL)
self.write(resp.body)
###############################
def main():
urls = [
("/api/data", AddData),
("/api/history", History),
]
return Application(urls, debug = True)
#################################
if __name__ == '__main__':
app = main()
app.listen(8888)
IOLoop.instance().start()