-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
49 lines (42 loc) · 1.56 KB
/
server.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
from tornado import gen, web
from tornado.process import Subprocess
from tornado.ioloop import IOLoop
from tornado.iostream import StreamClosedError
from tornado.websocket import WebSocketHandler
import json
# adapted from https://stackoverflow.com/questions/41431882/live-stream-stdout-and-stdin-with-websocket
class WSHandler(WebSocketHandler):
def check_origin(self, origin):
return True
def open(self):
print("WebSocket opened")
self.app = Subprocess(['/usr/bin/python3.8', '-u', self.pydd, self.args],
stdout=Subprocess.STREAM,
stdin=Subprocess.STREAM)
IOLoop.current().spawn_callback(self.stream_output)
def on_message(self, message):
print("receiving ", message)
message += "\n"
self.app.stdin.write(message.encode('utf-8'))
def on_close(self):
print("WebSocket closed")
@gen.coroutine
def stream_output(self):
try:
while True:
line = yield self.app.stdout.read_bytes(8092, partial=True)
line = line.decode("utf-8")
line = line.replace("\n", "")
print("line ", line)
self.write_message(line)
except StreamClosedError:
pass
def run_server(address='localhost', port=8001):
application = web.Application([
(r"/", WSHandler),
],
debug=True,
)
application.listen(port, address)
print('Server listening at http://localhost:8001/')
IOLoop.current().start()