Introduction
In this tutorial of my Python and Linux series, you will learn how to create Python code that retrieves the display server type of your Linux computer and returns the value as x11, wayland, or tty.
Starter Code
Start by creating a file called display_server.py .
To edit the file, I recommend using the free Visual Studio Code (VS Code). If you would like to set up Visual Studio Code for Python development, follow my tutorial.
On the first line of the Python file, import the os module:
import os
Next, set a variable for the current session id of the loginctl command:
session_id = os.popen("loginctl").read().replace("-", "").split()[5]
This code gets data from the loginctl command and then formats the results.
The next code pythonically gives the loginctl command the session id given from the last code snippet so that it can give back the type of display server your Linux computer is running.
session = (
os.popen(f"loginctl show-session {session_id} -p Type")
.read()
.lower()
.replace("type=", "")
.rstrip()
)
Finally, print the session variable to get the result.
print(session)
Your code should now be exactly like this:
import os
session_id = os.popen("loginctl").read().replace("-", "").split()[5]
session = (
os.popen(f"loginctl show-session {session_id} -p Type")
.read()
.lower()
.replace("type=", "")
.rstrip()
)
print(session)
Running Your Code
To run your code, open terminal and run this command:
python display_server.py
If you are using VS Code, you can optionally use the keyboard shortcut ctrl-~ (Control-Tilde) to open the VS Code Terminal and run the code there instead of opening the regular Linux terminal.
Improving Your Code
If you would like to improve your code a little bit, you can check whether the system is Linux or not and create a custom warning message, so that Windows and Mac users don’t run the code and get thrown default Python errors, but instead are given a warning message about the code only being able to run on Linux.
At the top of your Python file, below import os , add this code to import Platform and the Warnings module:
import platform
import warnings
Next, add the following code below your import statements to create a custom warning type of PlatformWarning:
class PlatformWarning(UserWarning):
pass
Next, add this code directly above the session_id variable to check whether the system running the code is Linux:
if platform.system() == "Linux":
Finally, add this code below print(session) to show a warning to non-Linux users.
else:
warnings.warn("Code should only be run on Linux.", PlatformWarning)
I have formatted the completed code including the advanced code and have included the result below:
import os
import platform
import warnings
class PlatformWarning(UserWarning):
pass
if platform.system() == "Linux":
session_id = os.popen("loginctl").read().replace("-", "").split()[5]
session = (
os.popen(f"loginctl show-session {session_id} -p Type")
.read()
.lower()
.replace("type=", "")
.rstrip()
)
print(session)
else:
warnings.warn("This code can only be run on Linux.", PlatformWarning)
Conclusion
That’s all there is to it… you have completed Python code that checks the type of display server on Linux computers. I hope you have enjoyed this article!
Check back on TitusJTech for more articles! 😎
