用Python ping一个网站? [英] Ping a site in Python?

查看:205
本文介绍了用Python ping一个网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Python ping网站或IP地址?

How do I ping a website or IP address with Python?

推荐答案

请参阅此 Matthew Dixon Cowles 纯Python ping . jensdiemer.de/"rel =" noreferrer> Jens Diemer .另外,请记住,Python需要root才能在Linux中生成ICMP(即ping)套接字.

See this pure Python ping by Matthew Dixon Cowles and Jens Diemer. Also, remember that Python requires root to spawn ICMP (i.e. ping) sockets in linux.

import ping, socket
try:
    ping.verbose_ping('www.google.com', count=3)
    delay = ping.Ping('www.wikipedia.org', timeout=2000).do()
except socket.error, e:
    print "Ping Error:", e

源代码本身易于阅读,请参见verbose_pingPing.do的实现以获取启发.

The source code itself is easy to read, see the implementations of verbose_ping and of Ping.do for inspiration.

这篇关于用Python ping一个网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆