Debian Codegraph @

examples/websocket.py

raw
1 import eventlet
2 from eventlet import wsgi
3 from eventlet import websocket
4
5 # demo app
6 import os
7 import random
8
9
10 @websocket.WebSocketWSGI
11 def handle(ws):
12 """ This is the websocket handler function. Note that we
13 can dispatch based on path in here, too."""
14 if ws.path == '/echo':
15 while True:
16 m = ws.wait()
17 if m is None:
18 break
19 ws.send(m)
20
21 elif ws.path == '/data':
22 for i in range(10000):
23 ws.send("0 %s %s\n" % (i, random.random()))
24 eventlet.sleep(0.1)
25
26
27 def dispatch(environ, start_response):
28 """ This resolves to the web page or the websocket depending on
29 the path."""
30 if environ['PATH_INFO'] == '/data':
31 return handle(environ, start_response)
32 else:
33 start_response('200 OK', [('content-type', 'text/html')])
34 return [open(os.path.join(
35 os.path.dirname(__file__),
36 'websocket.html')).read()]
37
38 if __name__ == "__main__":
39 # run an example app from the command line
40 listener = eventlet.listen(('127.0.0.1', 7000))
41 print("\nVisit http://localhost:7000/ in your websocket-capable browser.\n")
42 wsgi.server(listener, dispatch)