# uPing **Repository Path**: echo/uPing ## Basic Information - **Project Name**: uPing - **Description**: 使用 MicroPython 适配 ESP32 的 ping 函数,支持异步。 - **Primary Language**: Python - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2023-12-29 - **Last Updated**: 2023-12-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## µPing (MicroPing) for MicroPython (ESP32 version) > Fork from [coffee-it](https://github.com/coffee-it)/[uPing](https://github.com/coffee-it/uPing) . 使用默认设置运行 ```python import uping pinger = uping.Ping('example.org') pinger.start() ``` ``` PING example.org (93.184.216.34): 56 data bytes 64 bytes from 93.184.216.34: seq=0 ttl=54 time=106.261 ms 64 bytes from 93.184.216.34: seq=1 ttl=54 time=106.221 ms 64 bytes from 93.184.216.34: seq=2 ttl=54 time=106.421 ms 64 bytes from 93.184.216.34: seq=3 ttl=54 time=107.521 ms 4 packets transmitted, 4 packets received, 0 packet loss round-trip min/avg/max = 106.221/106.606/107.521 ms ``` --- ### 参数 Required - HOST:(FQDN(正式域名) or IP address) Optional - SOURCE:(default: None, ip address) - COUNT:(default: 4, integer) - INTERVAL:(default: 1000, ms) - SIZE:(default: 64, bytes) - TIMEOUT:(default: 5000, ms) - quiet:(default: False, bool) --- ### 类方法 #### Ping.start() > 使用给定的参数启动ping循环。并且总是从第一个序号开始。 > > 如果是静默的,则返回带有ping结果的元组: > > `result(tx=4, rx=4, losses=0, min=106.221, avg=106.606, max=107.521)` > > Where: > > `tx` - transmitted(传输), > > `rx` - received(接收), > > `losses` - percentage of packets that be lost(丢失数据包百分比). #### Ping.ping() > 只发送一个数据包。保持和增加当前序号。 ```python import uping pinger = uping.Ping('example.org') # Some awesome code pong = pinger.ping() print(pong) ``` > 返回元组:序列号(int)、往返时间(ms, float)、ttl(int)。 > > `(5, 106.521, 54)` #### Ping.ping_async() > 支持异步。通过 `Ping.getEND_async()` 获取数据为元祖:序列号(int)、往返时间(ms, float)、ttl(int)。 #### Ping.getEND_async() ```python import uping async def unraid_ip(): try: pinger = uping.Ping("www.baidu.com") await pinger.ping_async() print("PING Success...") print(pinger.getEND_async()) except: print('PING network except') finally: pinger.close() ``` #### Ping.close() > 关闭 socket 连接。