• <bdo id='XfMH2'></bdo><ul id='XfMH2'></ul>

    <small id='XfMH2'></small><noframes id='XfMH2'>

    <legend id='XfMH2'><style id='XfMH2'><dir id='XfMH2'><q id='XfMH2'></q></dir></style></legend>

    1. <i id='XfMH2'><tr id='XfMH2'><dt id='XfMH2'><q id='XfMH2'><span id='XfMH2'><b id='XfMH2'><form id='XfMH2'><ins id='XfMH2'></ins><ul id='XfMH2'></ul><sub id='XfMH2'></sub></form><legend id='XfMH2'></legend><bdo id='XfMH2'><pre id='XfMH2'><center id='XfMH2'></center></pre></bdo></b><th id='XfMH2'></th></span></q></dt></tr></i><div id='XfMH2'><tfoot id='XfMH2'></tfoot><dl id='XfMH2'><fieldset id='XfMH2'></fieldset></dl></div>

      <tfoot id='XfMH2'></tfoot>
    2. 为什么向 subprocess.Popen 提供标准输入会导致写入标准输出的内容发生变化?

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

        <i id='OPPhj'><tr id='OPPhj'><dt id='OPPhj'><q id='OPPhj'><span id='OPPhj'><b id='OPPhj'><form id='OPPhj'><ins id='OPPhj'></ins><ul id='OPPhj'></ul><sub id='OPPhj'></sub></form><legend id='OPPhj'></legend><bdo id='OPPhj'><pre id='OPPhj'><center id='OPPhj'></center></pre></bdo></b><th id='OPPhj'></th></span></q></dt></tr></i><div id='OPPhj'><tfoot id='OPPhj'></tfoot><dl id='OPPhj'><fieldset id='OPPhj'></fieldset></dl></div>
          <bdo id='OPPhj'></bdo><ul id='OPPhj'></ul>

          <small id='OPPhj'></small><noframes id='OPPhj'>

              <legend id='OPPhj'><style id='OPPhj'><dir id='OPPhj'><q id='OPPhj'></q></dir></style></legend>

                <tbody id='OPPhj'></tbody>
                <tfoot id='OPPhj'></tfoot>

              1. 本文介绍了为什么向 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 提供标准输入会导致写入标准输出的内容发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)

                1. <tfoot id='HCET4'></tfoot>

                    <small id='HCET4'></small><noframes id='HCET4'>

                    <legend id='HCET4'><style id='HCET4'><dir id='HCET4'><q id='HCET4'></q></dir></style></legend>

                      • <bdo id='HCET4'></bdo><ul id='HCET4'></ul>
                        <i id='HCET4'><tr id='HCET4'><dt id='HCET4'><q id='HCET4'><span id='HCET4'><b id='HCET4'><form id='HCET4'><ins id='HCET4'></ins><ul id='HCET4'></ul><sub id='HCET4'></sub></form><legend id='HCET4'></legend><bdo id='HCET4'><pre id='HCET4'><center id='HCET4'></center></pre></bdo></b><th id='HCET4'></th></span></q></dt></tr></i><div id='HCET4'><tfoot id='HCET4'></tfoot><dl id='HCET4'><fieldset id='HCET4'></fieldset></dl></div>

                          <tbody id='HCET4'></tbody>