2014-02-26 56 views
-1

我有以下代码来检查CNAME的URL列表。等效的bash命令是“dig mail.yahoo.com | grep CNAME”Python子流程模块和PIPE

'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代码得到所需的输出。我得到了低于输出。

$蟒蛇开发/ cname_check.py

mail.yahoo.com

gmail.com

google.com

$

请让我知道我是什么失踪。我从这里跟着例子,http://docs.python.org/dev/library/subprocess.html#replacing-shell-pipeline

谢谢。

回答

0

您打印host但不是output,这就是为什么您只能看到列出的主机。只需在最后添加print output ...

+0

谢谢你的工作!我还必须添加'string.strip(host)'来剥离文件的输出。 – Tinku