Ping在Python的网站?
如何使用Python ping网站或IP地址?
How do I ping a website or IP address with Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何使用Python ping网站或IP地址?
How do I ping a website or IP address with Python?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(18)
请参阅此 matthew dixon cowles 和 Jens Diemer 。另外,请记住,Python需要root才能在Linux中产生ICMP(IE ping)插座。
源代码本身很容易读取,请参阅
verbose_ping
和ping.do
的实现。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.
The source code itself is easy to read, see the implementations of
verbose_ping
and ofPing.do
for inspiration.根据您想实现的目标,您可能最容易调用系统ping命令。.
使用子过程模块是这样做的最佳方法,尽管您必须记住在不同的操作系统上ping命令有所不同!
您不必担心壳式字符。例如..
..将不会 执行回声命令。
现在,要获得PING结果,您可以解析
OUT
变量。示例输出:示例REGEX:
再次,请记住输出会根据操作系统(甚至是
ping
的版本)而变化。这不是理想的,但是在许多情况下都可以正常工作(您知道脚本将运行的机器)Depending on what you want to achieve, you are probably easiest calling the system ping command..
Using the subprocess module is the best way of doing this, although you have to remember the ping command is different on different operating systems!
You don't need to worry about shell-escape characters. For example..
..will not execute the echo command.
Now, to get the ping results, you could parse the
out
variable. Example output:Example regex:
Again, remember the output will vary depending on the operating system (and even the version of
ping
). This isn't ideal, but it will work fine in many situations (where you know the machines the script will be running on)您可能会发现 noah礼物的演示使用python创建敏捷命令行工具。在其中,他结合了子过程,队列和线程,以开发能够同时启动主机并加快过程的解决方案。以下是基本版本,然后他添加命令行解析和其他一些功能。可以找到此版本的代码在
这里 =“ noreferrer”> python for Unix和linux系统管理
href =“ https://rads.stackoverflow.com/amzn/click/com/0596515820” rel .com/amzn/click/com/0596515820" rel="noreferrer">http://ecx.images-amazon.com/images/I/515qmR%2B4sjL._SL500_AA240_.jpg
You may find Noah Gift's presentation Creating Agile Commandline Tools With Python. In it he combines subprocess, Queue and threading to develop solution that is capable of pinging hosts concurrently and speeding up the process. Below is a basic version before he adds command line parsing and some other features. The code to this version and others can be found here
He is also author of: Python for Unix and Linux System Administration
http://ecx.images-amazon.com/images/I/515qmR%2B4sjL._SL500_AA240_.jpg
很难说您的问题是什么,但是有一些选择。
如果您的意思是使用ICMP PING协议从字面上执行请求,则可以获取ICMP库并直接执行Ping请求。 Google“ python icmp”查找类似的东西 icmplib 。您可能想查看 scapy 也是如此。
这将比使用
os.System(“ ping” + ip)
要快得多。如果您的意思是一般“ ping”一个框以查看是否已经启动,则可以使用端口7上的回声协议。
对于Echo,您可以使用套接字库库打开IP地址和端口7。 \ n“ ),然后阅读答复。
如果您的意思是“ ping”网站以查看网站是否正在运行,则必须在端口80上使用HTTP协议。
对于或正确检查Web服务器,您可以使用 urllib2 打开特定的URL。 (
/index.html
总是很流行的)并阅读响应。“ ping”(包括“ Traceroute”和“手指”)还有更多的潜在含义。
It's hard to say what your question is, but there are some alternatives.
If you mean to literally execute a request using the ICMP ping protocol, you can get an ICMP library and execute the ping request directly. Google "Python ICMP" to find things like this icmplib. You might want to look at scapy, also.
This will be much faster than using
os.system("ping " + ip )
.If you mean to generically "ping" a box to see if it's up, you can use the echo protocol on port 7.
For echo, you use the socket library to open the IP address and port 7. You write something on that port, send a carriage return (
"\r\n"
) and then read the reply.If you mean to "ping" a web site to see if the site is running, you have to use the http protocol on port 80.
For or properly checking a web server, you use urllib2 to open a specific URL. (
/index.html
is always popular) and read the response.There are still more potential meaning of "ping" including "traceroute" and "finger".
最简单的答案是:
Most simple answer is:
我以这种方式做了类似的事情,这是一种灵感:
I did something similar this way, as an inspiration:
这是使用
subprocess
的简短片段。check_call
方法要么返回0以取得成功,要么提出异常。这样,我不必解析ping的输出。我正在使用shlex
来拆分命令行参数。Here's a short snippet using
subprocess
. Thecheck_call
method either returns 0 for success, or raises an exception. This way, I don't have to parse the output of ping. I'm usingshlex
to split the command line arguments.我开发了一个我认为可以为您提供帮助的图书馆。它称为ICMPLIB(与Internet上可以找到的同名其他任何代码无关),并且是Python中ICMP协议的纯粹实现。
它完全面向对象,并具有简单的功能,例如经典的ping,乘法和示踪剂,以及对于那些想要基于ICMP协议开发应用程序的人的低级类和插座。
以下是其他一些亮点:
要安装它(需要Python 3.6+):
这是ping函数的一个简单示例:
如果您想使用库中的库中,则将“特权”参数设置为false。
您可以在项目页面上找到完整的文档:
https://github.com/valentinbelyn/icmplib
希望您能找到这个库。
I develop a library that I think could help you. It is called icmplib (unrelated to any other code of the same name that can be found on the Internet) and is a pure implementation of the ICMP protocol in Python.
It is completely object oriented and has simple functions such as the classic ping, multiping and traceroute, as well as low level classes and sockets for those who want to develop applications based on the ICMP protocol.
Here are some other highlights:
To install it (Python 3.6+ required):
Here is a simple example of the ping function:
Set the "privileged" parameter to False if you want to use the library without root privileges.
You can find the complete documentation on the project page:
https://github.com/ValentinBELYN/icmplib
Hope you will find this library useful.
读取文件名,文件包含每行的一个URL,如以下:
use命令:
获取结果:
源代码(url.py):
read a file name, the file contain the one url per line, like this:
use command:
get the result:
source code(url.py):
如果您想在Python中真正玩一些东西,可以使用Scapy:
在我看来,这比某些时髦的子过程要好得多(并且完全跨平台)。另外,您可以像拥有数据包一样拥有有关答案(序列ID .....)的更多信息(序列ID .....)。
If you want something actually in Python, that you can play with, have a look at Scapy:
That's in my opinion much better (and fully cross-platform), than some funky subprocess calls. Also you can have as much information about the answer (sequence ID.....) as you want, as you have the packet itself.
在Python 3上,您可以使用 ping3 。
On python 3 you can use ping3.
使用系统ping命令ping ping osth of主机:
using system ping command to ping a list of hosts:
您可以在Windows和Linux 在这里
You can find an updated version of the mentioned script that works on both Windows and Linux here
使用子过程ping命令将其解码,因为响应是二进制的:
using subprocess ping command to ping decode it because the response is binary:
您可以尝试使用插座以获取网站的IP,并使用scrapy将ICMP ping授予IP。
you might try socket to get ip of the site and use scrapy to excute icmp ping to the ip.
如果您只想检查IP上的机器是否处于活动状态,则只能使用Python插座。
现在,根据显示的错误消息(或错误号码),您可以确定机器是否活动。
If you only want to check whether a machine on an IP is active or not, you can just use python sockets.
Now, according to the error message displayed (or the error number), you can determine whether the machine is active or not.
使用此功能已在Python 2.7上进行了测试,并且可以正常工作,如果成功并在失败时返回False,则以毫秒为单位返回PING时间。
Use this it's tested on python 2.7 and works fine it returns ping time in milliseconds if success and return False on fail.