Python 和 windows 中的 X11 连接

Python and X11 connection in windows

我正在尝试启动 X11 连接,从我的远程 Linux 服务器到我的本地 Windows 机器。

我已经下载了 Xming portable,如果我启动到我的 Linux 机器的 ssh 连接并启动 Firefox,它会被传递到 Xming 并显示在我的 Windows 机器上。

我现在已经尝试在 python 中实现相同的目标。但我不认为我理解正确。

我正在使用以下代码

import paramiko
import time

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('server-1', username='me', password='xxxxxxx')
stdin, stdout, stderr = ssh.exec_command("firefox")

t = ssh.get_transport ()
chan = t.open_session ()
print(chan.request_x11())

print(stdout.readlines(), stderr.readlines())

time.sleep(100)

只得到如下错误:

 Error: GDK_BACKEND does not match available displays

我还读到 python 它可以自行启动 Xll 会话。不过现在我只需要转发到我的Xming服务器即可。

我只了解 X11 连接的基本功能,我在这里看到的所有示例都是针对 python 脚本在 Linux 上的 运行 的情况。

此致

我自己想通了。虽然我不完全了解细节,但我猜 Xming 必须连接到 SSH 套接字或其他东西。无论如何,我需要做的就是将我的命令更改为以下内容

stdin, stdout, stderr = ssh.exec_command("export DISPLAY=localhost:10.0; xterm & firefox &")

Firefox 在 Xming 中打开。另请注意,python 脚本在此处被阻止,直到再次关闭 Firefox 应用程序。

所以我的最终代码如下所示

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('server-1', username='me', password='xxxx')
stdin, stdout, stderr = ssh.exec_command("export DISPLAY=localhost:10.0; xterm & firefox & env")


print(''.join(stdout.readlines()), ''.join(stderr.readlines()))

ssh.close()

并且由于 Xming 是一个命令行应用程序,因此也应该可以从 Python 应用程序内部启动它。