10
votes

How do I write to file descriptor 3 of a subprocess.Popen object?

I'm trying to accomplish the redirection in the following shell command with Python (without using named pipes):

$ gpg --passphrase-fd 3 -c 3<passphrase.txt < filename.txt > filename.gpg
1
I'm curious to know this. I don't think it's directly possible. The Popen objects offer handles to stdout, stdin and stderr. I don't know about others. - Noufal Ibrahim
Perhaps OT, but are you aware of the python-gnupg project which provides a Python API for GnuPG? See code.google.com/p/python-gnupg for more info. (Disclosure: it's my project) - Vinay Sajip
I looked into some Python gpg wrappers, and yours looks quite viable, but my current project is so small that I'm trying to minimize dependencies. - aaronstacy
I'm also interested in this. But it looks like python subprocess module is still not as capable as simple shell redirection. - Pavel Šimerda

1 Answers

7
votes

The subprocess proc inherits file descriptors opened in the parent process. So you can use os.open to open passphrase.txt and obtain its associated file descriptor. You can then construct a command which uses that file descriptor:

import subprocess
import shlex
import os

fd=os.open('passphrase.txt',os.O_RDONLY)
cmd='gpg --passphrase-fd {fd} -c'.format(fd=fd)
with open('filename.txt','r') as stdin_fh:
    with open('filename.gpg','w') as stdout_fh:        
        proc=subprocess.Popen(shlex.split(cmd),
                              stdin=stdin_fh,
                              stdout=stdout_fh)        
        proc.communicate()
os.close(fd)

To read from a pipe instead of a file, you could use os.pipe:

import subprocess
import shlex
import os

PASSPHRASE='...'

in_fd,out_fd=os.pipe()
os.write(out_fd,PASSPHRASE)
os.close(out_fd)
cmd='gpg --passphrase-fd {fd} -c'.format(fd=in_fd)
with open('filename.txt','r') as stdin_fh:
    with open('filename.gpg','w') as stdout_fh:        
        proc=subprocess.Popen(shlex.split(cmd),
                              stdin=stdin_fh,
                              stdout=stdout_fh )        
        proc.communicate()
os.close(in_fd)