简体   繁体   中英

Python and X11 connection in windows

I am trying to start and X11 connection from my remote Linux server to my Local Windows machine.

I've downloaded Xming portable and if I start an ssh connection to my Linux machine and starts Firefox it is passed to Xming and shown on my Windows machine.

I have now tried to achieve the same in python. But I do not think I understand it correctly.

I'm using the following code

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)

only to get the following error:

 Error: GDK_BACKEND does not match available displays

I've also read that python it self can start and Xll session. But for now I only need it to be forwarded to my Xming server.

I only understand the very basic of what an X11 connection does and all the examples I've seen here is for when the python script is running on Linux.

Regards

I figured it out my self. Although I do not fully understand the details but i guess Xming must be hooked into the SSH socket or something. Anyways all I needed to do was change my command to the following

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

and Firefox opens in Xming. Also note that the python script is blocked here until the Firefox app is closed again.

So my final code looks the following

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()

and since Xming is a command line app it should also be possible to launch it from inside the Python app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM