Debian Codegraph @

tests/manual/websocket-gunicorn.py

raw
1 import eventlet.websocket
2 import gunicorn
3 import os
4 import random
5 import sys
6
7
8 @eventlet.websocket.WebSocketWSGI
9 def wsapp(ws):
10 ws.send(b'test pass')
11 ws.close()
12
13
14 def app(environ, start_response):
15 body = b'''<!doctype html>
16 <h1 id=status>loading...</h1>
17 <script>
18 (function(D) {
19 ws = new WebSocket('ws://127.0.0.1:5001/');
20 ws.onmessage = function(msg) {
21 var fr = new FileReader();
22 fr.onload = function(ev) {
23 D.getElementById('status').innerHTML = ev.target.result;
24 }
25 fr.readAsText(msg.data);
26 };
27 ws.onerror = function() {
28 D.getElementById('status').innerHTML = 'test fail';
29 }
30 })(document);
31 </script>
32 '''
33 if environ.get('HTTP_UPGRADE') == 'websocket':
34 return wsapp(environ, start_response)
35
36 start_response(
37 '200 OK', (
38 ('Content-type', 'text/html'),
39 ('Content-Length', str(len(body))),
40 ('X-Gunicorn-Version', gunicorn.__version__),
41 ),
42 )
43 return [body]
44
45
46 if __name__ == '__main__':
47 cmd = 'gunicorn websocket-gunicorn:app -b 127.0.0.1:5001 -k eventlet -w 1'
48 sys.stderr.write('exec ' + cmd + '\n')
49 os.system(cmd)