如何使用本机python执行ping或traceroute? [英] How can I perform a ping or traceroute using native python?
问题描述
我希望能够从Python内部执行ping和traceroute,而不必执行相应的shell命令,所以我更喜欢本机python解决方案.
I would like to be able to perform a ping and traceroute from within Python without having to execute the corresponding shell commands so I'd prefer a native python solution.
推荐答案
出于安全考虑,经常以超级用户身份运行解释器(当然,您确实需要具有超级用户权限才能访问由ping和traceroute的ICMP规范!),但是如果您没有问题,这并不难-例如,页面具有针对ICMP的仍可用的基础代码(包括ping和traceroute),但它是为非常老的Python版本编写的,并且需要进行一些小改动才能与现代版本兼容,但是概念都存在,在我给您的两个URL中!
Running interpreters as root is often frowned upon on security grounds (and of course you DO need to have root permission to access the "raw" socked as needed by the ICMP specs of ping and traceroute!), but if you have no problems with that it's not hard -- e.g., this post gives a workable ping, and Jeremy Hylton's old page has still-usable underlying code for ICMP (both ping and traceroute) though it's written for very old Python versions and needs a litte facelift to shine with modern ones -- but, the concepts ARE all there, in both the URLs I gave you!
这篇关于如何使用本机python执行ping或traceroute?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!