I have tried an approach mentioned here using the following implementation:
from subprocess import PIPE, Popen
process = Popen(['/usr/bin/openssl', 'enc', '-aes-256-cbc', '-a', '-pass', 'pass:asdf'], stdin=PIPE, stdout=PIPE)
process.stdin.write('Hello this is it')
process.stdin.flush()
print(repr(process.stdout.readline()))
but it gets stuck at readline() although I have written and flushed already. I also tried a non-blocking approach mentioned here but that is also blocking on readline(). Following is my code for the later approach:
import sys
import time
from subprocess import PIPE, Popen
from threading import Thread
from Queue import Queue, Empty
def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close()
def write_to_stdin(process):
process.stdin.write(b'Hello this is it')
p = Popen(['/usr/bin/openssl', 'enc', '-aes-256-cbc', '-a', '-pass', 'pass:asdf'], stdin=PIPE, stdout=PIPE, bufsize=-1, close_fds=ON_POSIX)
q = Queue()
t2 = Thread(target=write_to_stdin, args=(p,))
t2.daemon = True
t2.start()
t = Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True # thread dies with the program
t.start()
try:
line = q.get(timeout=3) # or q.get(timeout=.1)
except Empty:
print('no output yet')
else:
print line
I get no output yet as output.
The only approach working is using:
process.communicate
but this closes the process and we have to re-open the process again. For a large number of messages to encrypt this is taking too much time and I am trying to avoid including any external package for achieving this task. Any help would be highly appreciated Thanks.