Skip to content

Instantly share code, notes, and snippets.

@nenetto
Created September 26, 2023 07:32
Show Gist options
  • Save nenetto/42f7cdae8c0d4ae42580c7193b90a529 to your computer and use it in GitHub Desktop.
Save nenetto/42f7cdae8c0d4ae42580c7193b90a529 to your computer and use it in GitHub Desktop.
Python subprocesses
def run_openssl(data):
env = os.environ.copy()
env[‘password’] = b’\xe24U\n\xd0Ql3S\x11’
proc = subprocess.Popen(
[‘openssl’, ‘enc’, ‘-des3’, ‘-pass’, ‘env:password’],
env=env,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
proc.stdin.write(data)
proc.stdin.flush() # Ensure the child gets input
return proc
# Run some processes and WAIT for them
procs = []
for _ in range(3):
data = os.urandom(10)
proc = run_openssl(data)
procs.append(proc)
for proc in procs:
out, err = proc.communicate() # Wait for them
print(out[-10:])
# Wait with timeout
proc = run_sleep(10)
try:
proc.communicate(timeout=0.1)
except subprocess.TimeoutExpired:
proc.terminate()
proc.wait()
print(‘Exit status’, proc.poll())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment