Paramiko模块始终会引发异常
paramiko.buffered_pipe.PipeTimeout
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_innerenter code here
self.run()
File "c:/Users/LENOVO/Desktop/index.py", line 42, in runenter code here
data = self.chan.recv(1024)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python37\lib\site-packages\paramiko\channel.py", line 685, in recv
raise socket.timeout()
socket.timeout
我正在使用paramiko,websocket,xterm.js构建一个webssh项目。
我尝试了Tornado和Flask,但是都抛出了socket.timeout异常
这是Flask代码
from flask import Flask, jsonify, request
from flask_sockets import Sockets
import paramiko
import threading
import time
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
app = Flask(__name__)
sockets = Sockets(app)
app.config['DEBUG'] = True
HOSTS = "172.16.100.100"
PORT = 22
USERNAME = "root"
PASSWORD = "123456"
@app.after_request
def after_request(response):
response.headers.add("Access-Control-Allow-Origin", "*")
if request.method == "OPTIONS":
response.headers["Access-Control-Allow-Methods"] = "DELETE, GET, POST, PUT"
headers = request.headers.get("Access-Control-Request-Headers")
if headers:
response.headers["Access-Control-Allow-Headers"] = headers
return response
class MyThread(threading.Thread):
def __init__(self, chan, ws):
threading.Thread.__init__(self)
self.chan = chan
self.ws = ws
def run(self):
while not self.chan.exit_status_ready():
time.sleep(0.1)
# try:
data = self.chan.recv(1024)
self.ws.send(data)
# except Exception as e:
# print("异常信息", str(e))
self.chan.sshclient.close()
return False
@sockets.route("/terminals/")
def terminals(ws):
sshclient = paramiko.SSHClient()
sshclient.load_system_host_keys()
sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshclient.connect(HOSTS, PORT, USERNAME, PASSWORD)
chan = sshclient.invoke_shell(term='xterm')
chan.settimeout(0)
thread = MyThread(chan, ws)
thread.setDaemon(True)
thread.start()
while not ws.closed:
message = ws.receive()
if message is not None:
chan.send(bytes(message, encoding='utf-8'))
if __name__ == '__main__':
server = pywsgi.WSGIServer(
('0.0.0.0', 5000), app, handler_class=WebSocketHandler)
server.serve_forever()
这是Javascript代码
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="./xterm.css" />
<script src="./xterm.js"></script>
<script src="./fit.js"></script>
<script src="./attach.js"></script>
</head>
<body>
<div id="terminal" style="height: 800px;"></div>
<script>
var term = new Terminal();
var socket = new WebSocket('ws://127.0.0.1:5000/terminals/');
term.open(document.getElementById('terminal'), true);
term.attach(socket, true, true);
term.detach(socket);
term._initialized = true
term.writeln('Welcome to xterm.js');
term.fit();
// term.on('key', (key, ev) => {
// if (key.charCodeAt(0) == 13)
// term.write('\n');
// term.write(key);
// });
socket.onclose = function () {
term.writeln("closed. Thank you for use!");
};
</script>
</body>
</html>
可以肯定的是,主机SSH连接正常。
建立连接后,data = self.chan.recv(1024)
是数据。
答案 0 :(得分:-1)
问题解决了
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="./xterm.css" />
<script src="./xterm.js"></script>
<script src="./fit.js"></script>
<script src="./attach.js"></script>
</head>
<body>
<div id="terminal" style="height: 800px;"></div>
<script>
var term = new Terminal({cursorBlink: true});
term.open(document.getElementById('terminal'), true);
term.writeln('Welcome to xterm.js');
var socket = new WebSocket('ws://127.0.0.1:5000/terminals/');
term.attach(socket, true, true);
term._initialized = true
term.fit();
socket.onclose = function () {
term.writeln("closed. Thank you for use!");
};
</script>
</body>
</html>