-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
謝謝。
謝謝你的工作!我還必須添加'string.strip(host)'來剝離文件的輸出。 – Tinku