Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Chris: Since you have a simple-ish example of using os.pipe thanks to Lars, I'll offer what <a href="https://github.com/ryran/pyrite/" rel="nofollow">Pyrite</a> (my GTK frontend for gpg) does as well in the hope that more code examples are better. My use case is a little more complicated than yours due to the gui aspect -- I actually use a dictionary for input and output, and I have code to launch gpg with stdin as input and code that launches it with files as input, among other complications.</p> <p>That warning said, I start with the gpg commandline in a list just like you do; however, instead of using <code>--passphrase-fd 0</code>, I create a custom file descriptor via <code>os.pipe()</code> to send the passphrase before loading the <code>Popen()</code> instance, which has <code>stdin=subprocess.PIPE</code> for the input data. Following are some relevant (modified) excerpts from pyrite's <a href="https://github.com/ryran/pyrite/blob/master/modules/crypt_interface.py" rel="nofollow">crypt_interface</a> module.</p> <pre><code>#!/usr/bin/env python # Adapted excerpts from Pyrite &lt;http://github.com/ryran/pyrite&gt; from subprocess import Popen, PIPE, check_output ... # I/O dictionary obj self.io = dict( stdin='', # Stores input text for subprocess stdout='', # Stores stdout stream from subprocess stderr=0, # Stores tuple of r/w file descriptors for stderr stream gstatus=0, # Stores tuple of r/w file descriptors for gpg-status stream infile=0, # Input filename for subprocess outfile=0) # Output filename for subprocess ... cmd = ['gpg'] fd_pwd_R, fd_pwd_W = os.pipe() os.write(fd_pwd_W, passwd) os.close(fd_pwd_W) cmd.append('--passphrase-fd') cmd.append(str(fd_pwd_R)) ... # If working direct with files, setup our Popen instance with no stdin if self.io['infile']: self.childprocess = Popen(cmd, stdout=PIPE, stderr=self.io['stderr'][3]) # Otherwise, only difference for Popen is we need the stdin pipe else: self.childprocess = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=self.io['stderr'][4]) # Time to communicate! Save output for later self.io['stdout'] = self.childprocess.communicate(input=self.io['stdin'])[0] # Clear stdin from our dictionary asap, in case it's huge self.io['stdin'] = '' # Close os file descriptors if fd_pwd_R: os.close(fd_pwd_R) time.sleep(0.1) # Sleep a bit to ensure everything gets read os.close(self.io['stderr'][5]) if self.io['gstatus']: os.close(self.io['gstatus'][6]) ... </code></pre> <p>The function that calls all that waits until the <code>self.childprocess</code> object has a <code>returncode</code> attribute and assuming the returncode was <code>0</code> and that the input was text (and not a file), it then reads gpg's stdout from that dictionary and prints it to the screen.</p> <p>Happy to answer questions or try to help from my limited experience. Can find my contact info by following links.</p> <p>Edit: You might also find <a href="https://github.com/ryran/b19scripts/blob/master/a4crypt.py" rel="nofollow">a4crypt</a> instructive as it is a much simpler frontend for gpg -- it was the project I started in order to learn python, and later mothballed after I "completed" (if there is such a thing) pyrite.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload