推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
LeeReamond
V2EX  ›  Python

关于 Python 的 popen 无法获取 pipe 的输出,大家有什么头猪吗

  •  
  •   LeeReamond · Dec 11, 2021 · 4073 views
    This topic created in 1789 days ago, the information mentioned may be changed or developed.

    使用 proc = popen("command")之后,使用 proc.stdout 获得标准输出流

    但是经过测试发现如果 command 里面是比较简单的命令,比如 ping baidu.com 这种那程序就能正常运行,如果是包含有 A 程序 pipe 到 B 的命令,比如 ffmpeg pipe 到 x264.exe 输出就完全无法拿到。请问各位大佬这是咋回事

    26 replies  •  2021-12-13 11:24:25 +08:00
    Quarter
    Quarter
    Dec 11, 2021 via iPhone❤️4
    1
    头猪,哈哈哈
    Seahurt
    Seahurt
    Dec 11, 2021
    2
    官方文档写的挺清楚的了,推荐先看文档。无法从 proc.stdout 获取输出可能原因是命令的输出是写到了 stderr ,需要用 stderr=subprocess.STDOUT 来重定向。另外 A pipe 到 B 的命令 A 的输出肯定就没有了,只能拿到 B 命令的输出,除非用了 tee
    chotow
    chotow
    Dec 11, 2021
    3
    难得见到一个不是拼音打字的朋友 🤣
    LeeReamond
    LeeReamond
    OP
    Dec 11, 2021
    4
    @Seahurt 想拿到 B 的输出,但是啥也没有啊
    vanton
    vanton
    Dec 11, 2021
    5
    ```python
    import shlex, subprocess
    import pprint

    command_line = "ping baidu.com | grep 'TTL'"
    args = shlex.split(command_line)

    # proc = subprocess.Popen(["ping","baidu.com"])
    proc = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE)

    pprint.pp(args)

    pprint.pp(proc.stdout.readlines())
    ```

    你跑下看有没有东西
    LeeReamond
    LeeReamond
    OP
    Dec 11, 2021
    7
    @vanton ping 命令不能正常工作,提示

    ping: usage error: Destination address required
    []
    vanton
    vanton
    Dec 11, 2021
    8
    @LeeReamond #7

    😱

    这都跑不起来?

    那你这环境有问题啊,你换台机子试下。
    hsfzxjy
    hsfzxjy
    Dec 11, 2021 via Android
    9
    看到头猪在想楼主是不是也逛 A 岛 :doge
    iluckypig
    iluckypig
    Dec 12, 2021
    10
    快过年了,杀猪吃肉!
    LeeReamond
    LeeReamond
    OP
    Dec 12, 2021
    11
    @vanton ssh 里的 ping 命令正常,我觉得是你代码传参有问题
    Salicylicacid
    Salicylicacid
    Dec 12, 2021 via iPhone
    12
    续( xfnd )
    Osk
    Osk
    Dec 12, 2021
    13
    @LeeReamond 上面 shell=True 的方式好像得注意 win/linux 平台的差异, 或者试试传递 args 为 string 试试.
    unix 中 list+shell=True 时, args 的其余参数好像是传给 shell
    Osk
    Osk
    Dec 12, 2021
    14
    On POSIX with shell=True, the shell defaults to /bin/sh. If args is a string, the string specifies the command to execute through the shell. This means that the string must be formatted exactly as it would be when typed at the shell prompt. This includes, for example, quoting or backslash escaping filenames with spaces in them. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself.

    On Windows with shell=True, the COMSPEC environment variable specifies the default shell. The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable.
    vanton
    vanton
    Dec 12, 2021
    15
    @LeeReamond #11
    这个在 win11 powershell 下写的,mac 不是这么写的。
    参数是 shlex 解析的,自动解析为当前操作系统可识别参数,所以参数没问题。

    一般就是你的环境问题。
    ClericPy
    ClericPy
    Dec 12, 2021
    16
    输出是 stderr 还是 stdout 上了? 反正这俩我都重定向到 PIPE 里然后迭代出来的
    ncepuzs
    ncepuzs
    Dec 12, 2021
    17
    @Salicylicacid 绪( xftj )
    huangzhiyia
    huangzhiyia
    Dec 12, 2021 via iPhone
    18
    二进制文件填写完整路径。如 ping 127.0.0.1 写成 /usr/bin/ping 127.0.0.1
    2i2Re2PLMaDnghL
    2i2Re2PLMaDnghL
    Dec 12, 2021
    19
    @vanton
    稍微 strace 了一下,你这段运行的是
    ["/bin/sh", "-c", "ping", "baidu.com", "|", "grep", "TTL"]
    确实是参数有问题,解析完了再给 shell=True 你这不是逗呢
    vanton
    vanton
    Dec 12, 2021
    20
    @2i2Re2PLMaDnghL #19

    你看了问题没,他这是 Windows 。
    LeeReamond
    LeeReamond
    OP
    Dec 13, 2021
    21
    @vanton 我正经 win10 系统,正经 python3.8 ,能有啥问题?而且我这里 ps 没有 grep
    vanton
    vanton
    Dec 13, 2021
    22
    @LeeReamond #21

    `pwsh -version`

    看下版本号,版本过低就会这样。现在 pwsh 版本是 7.2 。

    `ping baidu.com | findstr 'TTL'`

    确实版本太低又懒得升级,你跑下这个,就算预览版 pwsh 也能跑起来。

    要是还是跑不起来,就找个朋友换台机器试下。

    另外,`python --version` 也看下。
    LeeReamond
    LeeReamond
    OP
    Dec 13, 2021
    23
    @vanton 我的 powershell 没有 pwsh 命令,我的系统是 win10 21H2 企业版,这应该不是一个年久失修的系统。findstr 是有的,但是观察这个 pipe 似乎是要 ping 全部执行完才会推给 findstr ,而不是像 grep 一样一 ping 一打印
    vanton
    vanton
    Dec 13, 2021
    24
    @LeeReamond #23

    没有 pwsh 的话那应该是安装了最早版本的 powershell 。
    可以去 Microsoft store 装个新版的试下。
    https://www.microsoft.com/zh-cn/p/powershell/9mz1snwt0n5d?activetab=pivot:overviewtab

    Windows 用得不多,平时基本都是 mac 或者 linux 。
    如果还不行,无能为力。
    2i2Re2PLMaDnghL
    2i2Re2PLMaDnghL
    Dec 13, 2021
    25
    @vanton windows 你又 shell=True 又在谈 powershell 怎样怎样是不是有点毛病?
    要么别改 ComSpec ,要么别依赖 ComSpec 。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3836 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 66ms · UTC 05:32 · PVG 13:32 · LAX 21:32 · JFK 00:32
    ♥ Do have faith in what you're doing.