Process Execution Library
This module provides a nicer interface to the Python subprocess library.
How to use this library
Running processes
from xterminal import Process
# Run a process and get stdout, stderr and exit code of the process
p = Process("ifconfig")
stdout, stderr, exit_code = p.run()
# Print stdout when executing process
p = Process("ifconfig", print_stdout=True)
stdout, stderr, exit_code = p.run()
# Use a shell (this is necessary if you want to execute shell scripts on Linux or Batch scripts on Windows)
p = Process("ifconfig", shell=True)
stdout, stderr, exit_code = p.run()
Extract information from Process object
p = Process("ifconfig")
p.run()
# Get UUID of the process object
print(p.uuid)
print(p.get_uuid())
# Get stdout as list of lines
print(p.out)
print(p.get_output())
# Get error (if there was any)
print(p.err)
print(p.get_error())
# Get the environment variables of the process
print(p._env)
print(p.get_process_env())
# Check if process is finished
print(p.is_finished())
print(p.finished)
# Get exit code
print(p.get_exit_code())
print(p.exit_code)
# Get start time of process execution
print(p.get_start())
print(p.start)
# Get time when process was finished
print(p.get_end())
print(p.end)
Interact with stdout and stderr
def log_stdout(p: Process):
"""
This function is called everytime a line is read from stdout of the process
:param p: Process object
"""
print(f"[STDOUT] {p.get_last_line_from_stdout()}")
def log_stderr(p: Process):
"""
This function is called everytime a line is read from stderr of the process
:param p: Process object
"""
print(f"[STDOUT] {p.get_last_line_from_stderr()}")
p = Process("ifconfig")
p.register_stdout_readline_handler(log_stdout)
p.register_stderr_readline_handler(log_stderr)
p.run()
Even more interactivity: Write to stdin during process execution
def handle_questions(p: Process):
"""
This function is called everytime a line is read from stdout of the process
:param p: Process object
"""
if p.get_last_line_from_stdout() == "How old are you?":
p.write("30")
if p.get_last_line_from_stdout() == "Which country are you from?":
p.write("Germany")
p = Process("my_programm")
p.register_stdout_readline_handler(handle_questions)
p.run()
Running multiple commands via a terminal
from xterminal import Terminal
# Create a terminal
terminal = Terminal()
# Run commands
terminal.run("uname -a")
terminal.run("ls")
terminal.run("pwd")
# Inspect command history
for stdout, stderr, exitcode in terminal.history:
print("STDOUT:", stdout)
print("STDERR:", stderr)
print("EXITCODE:", exitcode)
# Run command in a different directory
terminal.run("ls", cwd="/")
# Run command with custom environment variables
terminal.run("printenv MY_VAR", env={"MY_VAR": "Hello World"})
# Run command with shell
terminal.run("echo $SHELL", shell=True)
# Run command without using OS environment variables
terminal.run("printenv", use_os_env=False)
# Run command with custom encoding
terminal.run("my_programm", encoding="utf-16")
# Run command with timeout
terminal.run("sleep 10", timeout=5)
# Run command and print stdout and stderr
terminal.run("ifconfig", print_stdout=True, print_stderr=True)
# Run command without printing stdout and stderr
terminal.run("ifconfig", print_stdout=False, print_stderr=False)
# Run command with a name
terminal.run("ifconfig", name="Network Configuration")
# Run command and inspect the Process object
process = terminal.run("ifconfig")
print("Process UUID:", process.get_uuid())
Running remote processes via SSH
from xterminal import SSHProcess
# Create an SSH client
client = SSHProcess.get_ssh_client(hostname="172.0.0.1", port=22, username="root", password="root")
# Create a process
process = SSHProcess(cmd="uname -a", ssh_client=client)
# Run the process
stdout, stderr, exitcode = process.run()
Run process in a different directory
# Run command 'ls' in directory '/'
process = SSHProcess(cmd="ls", cwd="/", ssh_client=client)
Create a SSH terminal to run multiple commands
from xterminal import SSHProcess, SSHTerminal
# Create an SSH client
client = SSHProcess.get_ssh_client(hostname="172.0.0.1", port=22, username="root", password="root")
# Create a terminal
terminal = SSHTerminal(ssh_client=client)
# Run commands
terminal.run("uname -a")
terminal.run("ls")
Inspect command history
All commands that were run in the terminal are saved in the history attribute of the terminal. This attribute contains a list of tuples (stdout, stderr, exitcode).
# Run commands
terminal.run("uname -a")
terminal.run("ls")
# Get history
terminal.history
Multi Client SSH Terminal
from xterminal import SSHProcess, MultiClientSSHTerminal
# Create an SSH client
client1 = SSHProcess.get_ssh_client(hostname="172.0.0.1", port=22, username="root", password="root")
client2 = SSHProcess.get_ssh_client(hostname="172.0.0.2", port=22, username="root", password="root")
# Create a terminal with multiple clients
terminal = MultiClientSSHTerminal(ssh_clients=[client1, client2])
Metadata
Release files for xterminal 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| xterminal-0.0.2.tar.gz | 8.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| xterminal-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.9 kB
Release files / xterminal-0.0.2.tar.gz
| Download URL | xterminal-0.0.2.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
04d4935c8af442f0129cf239eb53c45691c9f823f48d43c75e0ca64acacc7c70
|
|
BLAKE2b-256 checksum How to use checksums |
97988409dab6538802557c12cf290bebc03113bcc443243c9873c2f7fabf023c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.22
|
Release files / xterminal-0.0.2-py3-none-any.whl
| Download URL | xterminal-0.0.2-py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
18cfb50511f7ff80756f5a6335182db13f8fc10a3058ea2d46f2919efc5c8fcd
|
|
BLAKE2b-256 checksum How to use checksums |
cea08e4cad220c5ce19e04d36967c9da69d13760be0f9a1a75bcb640c5f67aaa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.22
|
Security for xterminal 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.