跳转到主要内容

使用原始套接字的异步Python ICMP ping实现。

项目描述

aio_ping

使用原始套接字的异步Python ping实现。

  • 与Python 3.5以上版本兼容。

  • 请注意,ICMP消息只能由以root身份运行的进程发送(在Windows上,您必须以“管理员”身份运行此脚本)。

原始版本

  • Matthew Dixon Cowles

  • 由python-ping团队copyleft 1989-2016,有关更多详细信息,请参阅AUTHORS

  • 许可证:GNU GPL v2,有关更多详细信息,请参阅LICENSE

用法

usage: ping [-h] [-w TIMEOUT] [-c COUNT] [-i INTERVAL] [-4] [-6]
            [-I SOURCEINTF] [-s NUMDATABYTES] [-T] [-S SOURCEIP]
            hostname

A pure python implementation of the ping protocol. *REQUIRES ROOT*

positional arguments:
  hostname              The address to attempt to ping.

optional arguments:
  -h, --help            show this help message and exit
  -w TIMEOUT, --deadline TIMEOUT
                        The maximum amount of time to wait until ping times
                        out.
  -c COUNT, --request_count COUNT
                        The number of attempts to make. Zero=infinite.
  -i INTERVAL, --interval INTERVAL
                        Time between ping attempts
  -4, --ipv4            Flag to use IPv4.
  -6, --ipv6            Flag to use IPv6.
  -I SOURCEINTF, --interface SOURCEINTF
                        Interface to use.
  -s NUMDATABYTES, --packet_size NUMDATABYTES
                        Designate the amount of data to send per packet.
  -T, --test_case       Flag to run the default test case suite.
  -S SOURCEIP, --source_address SOURCEIP
                        Source address from which ICMP Echo packets will be
                        sent.

作为库使用

# python3
>>> from aio_ping import ping
>>> ping('google.com', timeout=3000, count=3, delay=0.5)
True
>>> ping('google.com', timeout=3000, count=3, delay=0.5, verbose=True)

PYTHON PING google.com (216.58.212.46): 1300 data bytes
72 bytes from 216.58.212.46: icmp_seq=0 ttl=59 time=4.42 ms
72 bytes from 216.58.212.46: icmp_seq=1 ttl=59 time=4.70 ms
72 bytes from 216.58.212.46: icmp_seq=2 ttl=59 time=4.44 ms
72 bytes from 216.58.212.46: icmp_seq=3 ttl=59 time=4.47 ms

----216.58.212.46 PYTHON PING Statistics----
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip (ms)  min/avg/max = 4.4/4.5/4.7

1

异步使用是通过{Ping}类实现的,可以使用如下方式

from aio_ping import Ping,VerbosePing
async def ping(hostname, verbose=True, handle_signals=False, **kw):
    """
    Send @count ping to @hostname with the given @timeout
    """
    ping = (VerbosePing if verbose else Ping)(verbose=verbose, **kw)
    if handle_signals: ping.add_signal_handler()
    await ping.init(hostname)
    res = await ping.looped()
    if verbose:
        ping.print_stats()
    ping.close()
    return res

贡献

在GitHub上Fork此仓库发送拉取请求。谢谢。

修订历史

修订历史

项目详情


下载文件

下载适用于您的平台文件。如果您不确定要选择哪个,请了解有关安装软件包的更多信息。

源代码分发

aio_ping-0.1.3.tar.gz (10.4 kB 查看哈希值)

上传时间 源代码

由以下组织支持