Commit 299f0aa9 authored by ibuler's avatar ibuler

[Fixture] 完成terminal连接

parent 09dbf394
......@@ -57,5 +57,5 @@ class Luna(Flask, AppMixin):
async_mode = None
app = Luna(__name__, template_folder='dist')
socket_io = socketio.Server(logger=True, async_mode=async_mode)
socket_io = socketio.Server(logger=True, async_mode='threading')
app.wsgi_app = socketio.Middleware(socket_io, app.wsgi_app)
......@@ -59,13 +59,14 @@ def handle_term_connect(sid, environ):
def handle_machine(sid, message):
clients[sid]['host'] = host = '192.168.152.129'
clients[sid]['port'] = port = 22
# t = threading.Thread(target=forward, args=(sid,))
# t.setDaemon(True)
# t.start()
global thread
if thread is None:
thread = socket_io.start_background_task(forward, sid)
socket_io.emit('data', 'Connect to %s:%s' % (host, port), room=sid)
t = threading.Thread(target=forward, args=(sid,))
t.setDaemon(True)
t.start()
# global thread
# if thread is None:
# thread = socket_io.start_background_task(forward, sid)
socket_io.emit('data', 'Connect to %s:%s \r\n' % (host, port), room=sid)
print(t.is_alive())
@socket_io.on('data')
......@@ -83,8 +84,8 @@ def handle_term_disconnect(sid):
@socket_io.on('resize')
def handle_term_resize(sid, json):
print('term resize: ' + str(json))
pass
print(json)
def forward(sid):
......@@ -94,12 +95,6 @@ def forward(sid):
except KeyError as e:
socket_io.emit('data', e, room=sid)
return
for i in range(1, 10):
socket_io.emit('data', 'Forwarding\r\n')
socket_io.emit('data', 'Forwarding\r\n')
socket_io.sleep(1)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port=port, username='root', password='redhat')
......@@ -111,5 +106,4 @@ def forward(sid):
data = chan.recv(1024)
if not len(data):
break
print('Sending data: %s' % data)
socket_io.emit('data', 'What is')
socket_io.emit('data', data, room=sid)
......@@ -17,5 +17,19 @@ if __name__ == '__main__':
import eventlet
import eventlet.wsgi
eventlet.wsgi.server(eventlet.listen(('', 5000)), app)
elif socket_io.async_mode == 'gevent':
# deploy with gevent
from gevent import pywsgi
try:
from geventwebsocket.handler import WebSocketHandler
websocket = True
except ImportError:
websocket = False
if websocket:
pywsgi.WSGIServer(('', 5000), app,
handler_class=WebSocketHandler).serve_forever()
else:
pywsgi.WSGIServer(('', 5000), app).serve_forever()
else:
print('Unkonw async_mode: ' + socket_io.async_mode)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment