Created
September 26, 2023 07:32
-
-
Save nenetto/42f7cdae8c0d4ae42580c7193b90a529 to your computer and use it in GitHub Desktop.
Python subprocesses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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