首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python - traceroute和存储结果

问Python - traceroute和存储结果
EN

Stack Overflow用户
提问于 2016-07-30 00:29:22
回答 1查看 501关注 0票数 0

我需要执行一个简单的traceroute,并将结果存储在一个列表中,以供进一步的函数使用。

我在网上找到了一些东西,有些人在python上使用完整的脚本,有些人使用子过程,我认为后者会更容易,所以我尝试了我找到的一个简单的脚本

代码:

代码语言:javascript
复制
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')

但是,我得到了一个子异常

代码语言:javascript
复制
>>> 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
>>>

使用子进程是最简单的方法,还是应该使用脚本?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2020-03-04 11:09:39

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38663524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档