【问题标题】:Python subprocess module and PIPEPython 子进程模块和 PIPE
【发布时间】:2014-03-30 03:08:16
【问题描述】:

我有以下代码来检查 CNAME 以获取 URL 列表。等效的 bash 命令是“dig mail.yahoo.com |grep CNAME”

“hostname.txt”文件包含 URL 列表。

#!/usr/bin/python
from subprocess import Popen, PIPE
with open('hostname.txt') as hl:
    for host in hl:
            print host
            p1 = Popen(["dig", host], stdout=PIPE)
            p2 = Popen (["grep", "CNAME"], stdin=p1.stdout, stdout=PIPE)
            p1.stdout.close()
            output = p2.communicate()[0]

我没有从上面的 python 代码中得到想要的输出。我得到低于输出。

$python dev/cname_check.py

mail.yahoo.com

gmail.com

google.com

$

请让我知道我错过了什么。我按照这里的例子,http://docs.python.org/dev/library/subprocess.html#replacing-shell-pipeline

谢谢。

【问题讨论】:

    标签: python subprocess pipe


    【解决方案1】:

    您打印 host 而不是 output,这就是为什么您只看到列出的主机的原因。只需在末尾添加print output...

    【讨论】:

    • 感谢工作!我还必须添加 'string.strip(host)' 以从文件中删除输出。
    猜你喜欢
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多