This repository was archived by the owner on Jun 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-server.py
More file actions
78 lines (59 loc) · 2.15 KB
/
test-server.py
File metadata and controls
78 lines (59 loc) · 2.15 KB
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
import json
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
from tornado.ioloop import PeriodicCallback
import tornado.web
from random import randint # Random generator
# import Data
# Config
port = 7777 # Websocket Port
timeInterval = 10 # Milliseconds
class WSHandler(tornado.websocket.WebSocketHandler):
# check_origin fixes an error 403 with Tornado
# http://stackoverflow.com/questions/24851207/tornado-403-get-warning-when-opening-websocket
def check_origin(self, origin):
return True
def open(self):
# Send message periodic via socket upon a time interval
self.callback = PeriodicCallback(self.send_values, timeInterval)
self.callback.start()
def send_values(self):
# Generates random values to send via websocket
info = [0, randint(0, 70)]
to_send_info = json.dumps(info)
print(to_send_info)
self.write_message(to_send_info)
# Generates random values to send via websocket
info = [1, randint(0, 70)]
to_send_info = json.dumps(info)
print(to_send_info)
self.write_message(to_send_info)
# Generates random values to send via websocket
info = [2, randint(0, 70)]
to_send_info = json.dumps(info)
print(to_send_info)
self.write_message(to_send_info)
# Generates random values to send via websocket
info = [3, randint(0, 70)]
to_send_info = json.dumps(info)
print(to_send_info)
self.write_message(to_send_info)
def on_message(self, message):
pass
def on_close(self):
self.callback.stop()
def InputData(self, InputListe):
SinputList = []
for line in InputListe:
# he call .decode('ascii') converts the raw bytes to a string.
# .split(',') splits the string on commas.
s = line.decode("utf-8").split(',')
SinputList.append(s)
return SinputList
application = tornado.web.Application([
(r'/service', WSHandler),
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(port)
tornado.ioloop.IOLoop.instance().start()