我需要执行一个简单的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
>>>使用子进程是最简单的方法,还是应该使用脚本?
谢谢
发布于 2020-03-04 11:09:39
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 systemhttps://stackoverflow.com/questions/38663524
复制相似问题