Search code examples
pythonsshpyqt4xserver

How to determine from a python application if X server/X forwarding is running?


I'm writing a linux application which uses PyQt4 for GUI and which will only be used during remote sessions
(ssh -XY / vnc).

So sometimes it may occur that a user will forget to run ssh with X forwarding parameters or X forwarding will be unavailable for some reason. In this case the application crashes badly (unfortunately I am force to use an old C++ library wrapped into python and it completely messes user's current session if the application crashes).

I cannot use something else so my idea is to check if X forwarding is available before loading that library. However I have no idea how to do that.

I usually use xclock to check if my session has X forwarding enabled, but using xclock sounds like a big workaround.

ADDED
If possible I would like to use another way than creating an empty PyQt window and catching an exception.


Solution

  • Check to see that the $DISPLAY environment variable is set - if they didn't use ssh -X, it will be empty (instead of containing something like localhost:10).