Files
PyVtT/send_cmd.py
Max P. 210e0e95fb Refactors command sending and adds CLI support
Introduces argument parsing for command-line interaction, allowing
users to specify commands dynamically. Refactors the command
sending function to include detailed error handling and support
for configurable socket paths via external configuration.
2025-03-24 14:15:28 +01:00

54 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python3
import socket
import sys
import argparse
from configuration import read_configurations
CONFIGURATION = read_configurations()
def send_cmd(cmd: str, socket_path: str):
"""
Sends a command to a Unix domain socket server.
This function creates a Unix domain socket, connects to the server
specified by the socket_path, and sends the provided command as a
UTF-8 encoded string.
Args:
cmd (str): The command to send to the server.
socket_path (str): The path to the Unix domain socket.
Raises:
FileNotFoundError: If the socket file specified by socket_path does not exist.
ConnectionRefusedError: If the connection to the server is refused.
OSError: For other socket-related errors.
"""
try:
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client:
client.connect(socket_path)
client.sendall(cmd.encode())
except FileNotFoundError:
print(f"Error: The socket file '{socket_path}' does not exist.", file=sys.stderr)
except ConnectionRefusedError:
print(f"Error: Connection to the server at '{socket_path}' was refused.", file=sys.stderr)
except OSError as e:
print(f"Socket error: {e}", file=sys.stderr)
def main():
parser = argparse.ArgumentParser(
description="Send a command to a Unix domain socket server."
)
parser.add_argument(
"command",
choices=["start", "stop", "toggle"],
nargs="?",
default="toggle",
help="The command to send to the server (default: toggle).",
)
args = parser.parse_args()
send_cmd(args.command, CONFIGURATION["socket_path"])
if __name__ == "__main__":
main()