为什么将stdin提供给subprocess.Popen会导致写入stdout的内容发生变化? [英] Why does supplying stdin to subprocess.Popen cause what is written to stdout to change?

查看:240
本文介绍了为什么将stdin提供给subprocess.Popen会导致写入stdout的内容发生变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python的subprocess.Popen使用主机操作系统的二进制客户端执行一些FTP.由于各种原因,我无法使用ftplib或任何其他库.

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.

如果我将stdin处理程序附加到Popen实例,则二进制文件的行为似乎会改变.例如,使用XP的ftp客户端,该客户端接受发出命令的文本文件:

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:

>>>from subprocess import Popen, PIPE  
>>>p = Popen(['ftp','-A','-s:commands.txt','example.com'], stdout=PIPE)  
>>>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  
<snip>
ftp> binary  
200 Type set to I  
ftp> 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> quit  
>>>

commands.txt:

commands.txt:

binary  
get /testfiles/100.KiB  
quit  

同时提供stdin时,您在stdout中获得的一切就是:

When also supplying stdin, all you get in stdout is:

>>>from subprocess import Popen, PIPE  
>>>p = Popen(['ftp','-A','-s:commands.txt','example.com'], stdin=PIPE, stdout=PIPE)  
>>>p.communicate()[0]  
'binary  
get /testfiles/100.KiB  
quit'  
>>>

最初,我认为这是XP ftp客户端的一个怪癖,也许知道它不是处于交互模式,因此限制了它的输出.但是,OS X的ftp也会发生相同的行为-如果提供了stdin,则stdout会丢失所有服务器响应-这使我认为这是正常的行为.

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.

在Windows中,我可以使用-s开关来有效地编写ftp脚本,而无需使用stdin,但是在其他平台上,这种交互取决于外壳程序.

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.

两个平台上的Python版本均为2.6.x.为什么为stdin提供句柄会更改stdout,并且服务器响应会发送到哪里?

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?

推荐答案

我想我在某处(但不记得在哪里)看到Windows ftp客户端来自原始BSD实现之一.这样肯定会与Mac OS X的ftp实现共享一些关系.

I think I read somewhere (but can't remember where) that Windows ftp client came from one of the original BSD implementations. In that it would certainly shares some relationship with Mac OS X's ftp implementation.

对我来说,这与Popen无关,而与客户端ftp程序实现有关,后者使用isatty( 3)正如伊格纳西奥(Ignacio)在回答中提到的那样.对于可以在两种情况下都使用的程序,这是常见的做法.一个著名的例子是--color = auto选项的GNU grep实现:仅当stdout是tty时,它才会着色输出,而如果grep的输出通过管道传递到另一个命令中,则不会着色.

For me, this is not related to Popen but to the client ftp program implementation, which makes some checks about the context in which it is launched (to see if it's interacting with a human or a shell script), using isatty(3) as mentionned with Ignacio in his answer. This is common practise for programs which can be used in both context. A well known example is GNU grep implementation for the --color=auto option : it will colorize output only if stdout is a tty, and not if the output of grep is piped into another command.

这篇关于为什么将stdin提供给subprocess.Popen会导致写入stdout的内容发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆