我需要执行一个简单的traceroute并将结果存储在列表中以获得更多功能。
我在网上发现了一些东西,有些人在python上使用完整的脚本,有些人使用子进程,我认为后者会更容易,所以我尝试了一个简单的脚本我找到了
代码:
from subprocess import Popen, PIPE
import sys
def tracer(host=None):
p = Popen(['traceroute', host], stdout=PIPE)
while True:
try:
line = p.stdout.readline()
if not line:
break
print (line.rstrip())
except:
break
tracer('8.8.8.8')
但是我得到了一个孩子异常
>>> tracer('8.8.8.8')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<console>", line 2, in tracer
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
>>>
使用子进程是最简单的方法,还是应该使用脚本?
由于
答案 0 :(得分:1)
from subprocess import Popen, PIPE
import sys
def tracer(host=None):
p = Popen(['tracert', host], stdout=PIPE)
while True:
try:
line = p.stdout.readline()
if not line:
break
print (line.rstrip())
except:
break
tracer('8.8.8.8')```
had the same issue i changed once bit because the code was made for linux systems and you are using a windows system