关于 Python 的 popen 无法获取 pipe 的输出,大家有什么头猪吗 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 2021-12-11 19:26:17 +08:00 3342 次点击
    这是一个创建于 1452 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    26 条回复    2021-12-13 11:24:25 +08:00
    Quarter
        1
    Quarter  
       2021-12-11 19:41:21 +08:00 via iPhone   4
    头猪,哈哈哈
    Seahurt
        2
    Seahurt  
       2021-12-11 20:36:46 +08:00
    官方文档写的挺清楚的了,推荐先看文档。无法从 proc.stdout 获取输出可能原因是命令的输出是写到了 stderr ,需要用 stderr=subprocess.STDOUT 来重定向。另外 A pipe 到 B 的命令 A 的输出肯定就没有了,只能拿到 B 命令的输出,除非用了 tee
    chotow
        3
    chotow  
       2021-12-11 0:49:14 +08:00
    难得见到一个不是拼音打字的朋友
    LeeReamond
        4
    LeeReamond  
    OP
       2021-12-11 20:50:42 +08:00
    @Seahurt 想拿到 B 的输出,但是啥也没有啊
    vanton
        5
    vanton  
       2021-12-11 21:08:13 +08:00
    ```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
        7
    LeeReamond  
    OP
       2021-12-11 21:18:53 +08:00
    @vanton ping 命令不能正常工作,提示

    ping: usage error: Destination address required
    []
    vanton
        8
    vanton  
       2021-12-11 21:40:41 +08:00
    @LeeReamond #7



    这都跑不起来?

    那你这环境有问题啊,你换台机子试下。
    hsfzxjy
        9
    hsfzxjy  
       2021-12-11 22:30:06 +08:00 via Android
    看到头猪在想楼主是不是也逛 A 岛 :doge
    iluckypig
        10
    iluckypig  
       2021-12-12 00:16:07 +08:00
    快过年了,杀猪吃肉!
    LeeReamond
        11
    LeeReamond  
    OP
       2021-12-12 01:14:33 +08:00
    @vanton ssh 里的 ping 命令正常,我觉得是你代码传参有问题
    Salicylicacid
        12
    Salicylicacid  
       2021-12-12 09:06:27 +08:00 via iPhone
    续( xfnd )
    Osk
        13
    Osk  
       2021-12-12 10:57:46 +08:00
    @LeeReamond 上面 shell=True 的方式好像得注意 win/linux 平台的差异, 或者试试传递 args 为 string 试试.
    unix 中 list+shell=True 时, args 的其余参数好像是传给 shell
    Osk
        14
    Osk  
       2021-12-12 10:59:04 +08:00
    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
        15
    vanton  
       2021-12-12 11:50:10 +08:00
    @LeeReamond #11
    这个在 win11 powershell 下写的,mac 不是这么写的。
    参数是 shlex 解析的,自动解析为当前操作系统可识别参数,所以参数没问题。

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

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

    `pwsh -version`

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

    `ping baidu.com | findstr 'TTL'`

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

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

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

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

    Windows 用得不多,平时基本都是 mac 或者 linux 。
    如果还不行,无能为力。
    2i2Re2PLMaDnghL
        25
    2i2Re2PLMaDnghL  
       2021-12-13 10:04:25 +08:00
    @vanton windows 你又 shell=True 又在谈 powershell 怎样怎样是不是有点毛病?
    要么别改 ComSpec ,要么别依赖 ComSpec 。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5160 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 29ms UTC 01:23 PVG 09:23 LAX 17:23 JFK 20:23
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86