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

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

问题描述

我正在使用 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.

如果我将标准输入处理程序附加到 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  

同时提供标准输入时,您在标准输出中得到的只是:

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 上——如果提供了标准输入,所有服务器响应都从标准输出中丢失——这让我认为这是正常行为.

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 脚本,但在其他平台上,这种交互依赖于 shell.

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.为什么为标准输入提供句柄会改变标准输出,以及服务器响应去了哪里?

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)正如伊格纳西奥在他的回答中提到的那样.这是可以在两种情况下使用的程序的常见做法.一个众所周知的例子是 --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.

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

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