python 實作ping測試延遲的兩種方法,位元組,高于,主機,位址,大端
python 實作ping測試延遲的兩種方法
易采站長站,站長之家為您整理了python 實作ping測試延遲的兩種方法的相關内容。
一.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 ')
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測試延遲的資料請關注易采站長站其它相關文章!以上就是關于對python 實作ping測試延遲的兩種方法的詳細介紹。歡迎大家對python 實作ping測試延遲的兩種方法内容提出寶貴意見