一.python实现ping返回延迟繁琐版
#!/usr/bin/python3.7 # !coding:utf-8 __author__ = 'hsz' __date__ = 'Thu Feb 27 22:41:15 EST 2020' import time import struct import socket import select import sys def chesksum(data): """ 校验 """ n = len(data) m = n % 2 sum = 0 for i in range(0, n - m, 2): sum += (data[i]) + ((data[i + 1]) << 8) # 传入data以每两个字节(十六进制)通过ord转十进制,第一字节在低位,第二个字节在高位 if m: sum += (data[-1]) # 将高于16位与低16位相加 sum = (sum 16) + (sum & 0xffff) sum += (sum 16) # 如果还有高于16位,将继续与低16位相加 answer = ~sum & 0xffff # 主机字节序转网络字节序列(参考小端序转大端序) answer = answer 8 | (answer << 8 & 0xff00) return answer ''' 连接套接字,并将数据发送到套接字 ''' def raw_socket(dst_addr, imcp_packet): rawsocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp")) send_request_ping_time = time.time() # send data to the socket rawsocket.sendto(imcp_packet, (dst_addr, 80)) return send_request_ping_time, rawsocket, dst_addr ''' request ping ''' def request_ping(data_type, data_code, data_checksum, data_ID, data_Sequence, payload_body): # 把字节打包成二进制数据 imcp_packet = struct.pack('>BBHHH32s', data_type, data_code, data_checksum, data_ID, data_Sequence, payload_body) icmp_chesksum = chesksum(imcp_packet) # 获取校验和 imcp_packet = struct.pack('>BBHHH32s', data_type, data_code, icmp_chesksum, data_ID, data_Sequence, payload_body) return imcp_packet ''' reply ping ''' def reply_ping(send_request_ping_time, rawsocket, data_Sequence, timeout=2): while True: started_select = time.time() what_ready = select.select([rawsocket], [], [], timeout) wait_for_time = (time.time() - started_select) if what_ready[0] == []: # Timeout return -1 time_received = time.time() received_packet, addr = rawsocket.recvfrom(1024) icmpHeader = received_packet[20:28] type, code, checksum, packet_id, sequence = struct.unpack( ">BBHHH", icmpHeader ) if type == 0 and sequence == data_Sequence: return time_received - send_request_ping_time timeout = timeout - wait_for_time if timeout <= 0: return -1 ''' 实现 ping 主机/ip ''' def ping(host): data_type = 8 # ICMP Echo Request data_code = 0 # must be zero data_checksum = 0 # "...with value 0 substituted for this field..." data_ID = 0 # Identifier data_Sequence = 1 # Sequence number payload_body = b'abcdefghijklmnopqrstuvwabcdefghi' # data dst_addr = socket.gethostbyname(host) # 将主机名转ipv4地址格式,返回以ipv4地址格式的字符串,如果主机名称是ipv4地址,则它将保持不变 print("正在 Ping {0} [{1}] 具有 32 字节的数据:".format(host, dst_addr)) for i in range(0, 4): icmp_packet = request_ping(data_type, data_code, data_checksum, data_ID, data_Sequence + i, payload_body) send_request_ping_time, rawsocket, addr = raw_socket(dst_addr, icmp_packet) times = reply_ping(send_request_ping_time, rawsocket, data_Sequence + i) if times > 0: print("来自 {0} 的回复: 字节=32 时间={1}ms".format(addr, int(times * 1000))) time.sleep(0.7) else: print("请求超时。") if __name__ == "__main__": # if len(sys.argv) < 2: # sys.exit('Usage: ping.py <host>') ping('www.baidu.com') # sys.argv[1]
二 .python实现ping返回延迟简单版本
from ping3 import ping def ping_host(ip): """ 获取节点的延迟的作用 :param node: :return: """ ip_address = ip response = ping(ip_address) print(response) if response is not None: delay = int(response * 1000) print(delay, "延迟") # 下面两行新增的 ping_host('www.baidu.com')
以上就是python 实现ping测试延迟的两种方法的详细内容,更多关于python ping测试延迟的资料请关注其它相关文章!
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月23日
2024年11月23日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]