Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does supplying stdin to subprocess.Popen cause what is written to stdout to change?
    primarykey
    data
    text
    <p>I'm using Python's subprocess.Popen to perform some FTP using the binary client of the host operating system. I can't use ftplib or any other library for various reasons.</p> <p>The behavior of the binary seems to change if I attach a stdin handler to the Popen instance. For example, using XP's ftp client, which accepts a text file of commands to issue:</p> <pre><code>&gt;&gt;&gt;from subprocess import Popen, PIPE &gt;&gt;&gt;p = Popen(['ftp','-A','-s:commands.txt','example.com'], stdout=PIPE) &gt;&gt;&gt;p.communicate()[0] 'Connected to example.com. 220 ProFTPD 1.3.1 Server (Debian) ... 331 Anonymous login ok, send your complete email address as your password &lt;snip&gt; ftp&gt; binary 200 Type set to I ftp&gt; get /testfiles/100.KiB 200 PORT command successful 150 Opening BINARY mode data connection for /testfiles/100.KiB (102400 bytes) 226 Transfer complete ftp: 102400 bytes received in 0.28Seconds 365.71Kbytes/sec. ftp&gt; quit &gt;&gt;&gt; </code></pre> <p>commands.txt:</p> <pre><code>binary get /testfiles/100.KiB quit </code></pre> <p>When also supplying stdin, all you get in stdout is:</p> <pre><code>&gt;&gt;&gt;from subprocess import Popen, PIPE &gt;&gt;&gt;p = Popen(['ftp','-A','-s:commands.txt','example.com'], stdin=PIPE, stdout=PIPE) &gt;&gt;&gt;p.communicate()[0] 'binary get /testfiles/100.KiB quit' &gt;&gt;&gt; </code></pre> <p>Initially I thought this was a quirk of the XP ftp client, perhaps knowing it wasn't in interactive mode and therefore limiting its output. However, the same behaviour happens with OS X's ftp - all the server responses are missing from stdout if stdin is supplied - which leads me to think that this is normal behaviour.</p> <p>In Windows I can use the -s switch to effectively script ftp without using stdin, but on other platforms one relies on the shell for that kind of interaction.</p> <p>Python version is 2.6.x on both platforms. Why would supplying a handle for stdin change stdout, and where have the server responses gone to?</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.
 

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