Skip to content

Instantly share code, notes, and snippets.

@sxlijin
Created September 30, 2024 18:06
Show Gist options
  • Save sxlijin/b879e8cd0b79e7cd0e3b74a2088875af to your computer and use it in GitHub Desktop.
Save sxlijin/b879e8cd0b79e7cd0e3b74a2088875af to your computer and use it in GitHub Desktop.
python subprocess uds notes

in one terminal:

python processor.py

in another terminal:

nc -U /tmp/my_unix_socket

and in the nc window, type asdf and press enter

import socket
import subprocess
import os
import time
# Define the path for the Unix domain socket
SOCKET_PATH = "/tmp/unix_domain_socket_example"
# Ensure the socket doesn't already exist
if os.path.exists(SOCKET_PATH):
os.unlink(SOCKET_PATH)
# Function to start a subprocess that listens on the Unix domain socket
def start_subprocess():
# Start a subprocess that listens on the Unix socket
return subprocess.Popen(
['sh', '-c', f"""
while true; do
if [ -S "{SOCKET_PATH}" ]; then
nc -lU "{SOCKET_PATH}" | while read line; do
echo "Received: $line"
sleep 1
done
fi
done
"""],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Create a Unix domain socket
server_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server_socket.bind(SOCKET_PATH)
server_socket.listen(1)
# Start the subprocess
subprocess_listener = start_subprocess()
print("Subprocess started and listening on the Unix domain socket...")
# Connect to the Unix domain socket as a client
client_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
client_socket.connect(SOCKET_PATH)
print("")
# Send a request to the subprocess through the socket
client_socket.sendall(b"Hello from the client\n")
# Receive streaming output from the subprocess
def receive_stream():
while True:
output = subprocess_listener.stdout.readline()
if output == b"" and subprocess_listener.poll() is not None:
break
if output:
print(f"Subprocess output: {output.decode().strip()}")
# Start receiving the stream output
try:
receive_stream()
finally:
# Cleanup
client_socket.close()
server_socket.close()
subprocess_listener.terminate()
os.unlink(SOCKET_PATH)
print("Sockets closed and subprocess terminated.")
import socket
import os
# Specify the path for the Unix domain socket
SOCKET_PATH = "/tmp/my_unix_socket"
# Ensure the socket path doesn't already exist
if os.path.exists(SOCKET_PATH):
os.unlink(SOCKET_PATH)
# Create a Unix domain socket
server_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# Bind the socket to the specified path
server_socket.bind(SOCKET_PATH)
# Start listening for incoming connections
server_socket.listen(1)
print(f"Listening on {SOCKET_PATH}")
try:
# Wait for a client to connect
connection, client_address = server_socket.accept()
print("Client connected")
# Stream data back to the client
with connection:
while True:
# Receive data from the client
data = connection.recv(1024)
if not data:
break
print(f"Received: {data.decode().strip()}")
# Process data and send a response back to the client
response = f"Echoing back: {data.decode().strip()}\n"
connection.sendall(response.encode())
print(f"Sent: {response.strip()}")
finally:
# Cleanup
server_socket.close()
os.unlink(SOCKET_PATH)
print("Socket closed and cleaned up.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment