-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
53 lines (42 loc) · 1.36 KB
/
server.py
File metadata and controls
53 lines (42 loc) · 1.36 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
"""
Serve webcam images from a Redis store using Tornado.
Usage:
python server.py
"""
import base64
import time
import redis
from tornado import websocket, web, ioloop
MAX_FPS = 100
class IndexHandler(web.RequestHandler):
""" Handler for the root static page. """
def get(self):
""" Retrieve the page content. """
self.render('index.html')
class SocketHandler(websocket.WebSocketHandler):
""" Handler for the websocket URL. """
def __init__(self, *args, **kwargs):
""" Initialize the Redis store and framerate monitor. """
super().__init__(*args, **kwargs)
self._store = redis.Redis()
self._prev_image_id = None
def on_message(self, message):
""" Retrieve image ID from database until different from last ID,
then retrieve image, de-serialize, encode and send to client. """
while True:
time.sleep(1./MAX_FPS)
image_id = self._store.get('image_id')
if image_id != self._prev_image_id:
break
self._prev_image_id = image_id
image = self._store.get('image')
image = base64.b64encode(image)
self.write_message(image)
print(image_id)
app = web.Application([
(r'/', IndexHandler),
(r'/ws', SocketHandler),
])
if __name__ == '__main__':
app.listen(9000)
ioloop.IOLoop.instance().start()