python實(shí)現(xiàn)一個(gè)簡單的ping工具方法
繼上一篇計(jì)算checksum校驗(yàn)和,本章通過socket套接字,struct字節(jié)打包成二進(jìn)制,select返回套接字的文件描述符的結(jié)合,實(shí)現(xiàn)一個(gè)簡單的ping工具。
#!/usr/bin/python3.6.4
#!coding:utf-8
__author__ = 'Rosefinch'
__date__ = '2018/5/31 22:27'
import time
import struct
import socket
import select
import sys
def chesksum(data):
"""
校驗(yàn)
"""
n = len(data)
m = n % 2
sum = 0
for i in range(0, n - m ,2):
sum += (data[i]) + ((data[i+1]) << 8)#傳入data以每兩個(gè)字節(jié)(十六進(jìn)制)通過ord轉(zhuǎn)十進(jìn)制,第一字節(jié)在低位,第二個(gè)字節(jié)在高位
if m:
sum += (data[-1])
#將高于16位與低16位相加
sum = (sum >> 16) + (sum & 0xffff)
sum += (sum >> 16) #如果還有高于16位,將繼續(xù)與低16位相加
answer = ~sum & 0xffff
#主機(jī)字節(jié)序轉(zhuǎn)網(wǎng)絡(luò)字節(jié)序列(參考小端序轉(zhuǎn)大端序)
answer = answer >> 8 | (answer << 8 & 0xff00)
return answer
'''
連接套接字,并將數(shù)據(jù)發(fā)送到套接字
'''
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):
#把字節(jié)打包成二進(jìn)制數(shù)據(jù)
imcp_packet = struct.pack('>BBHHH32s',data_type,data_code,data_checksum,data_ID,data_Sequence,payload_body)
icmp_chesksum = chesksum(imcp_packet)#獲取校驗(yàn)和
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
'''
實(shí)現(xiàn) ping 主機(jī)/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)#將主機(jī)名轉(zhuǎn)ipv4地址格式,返回以ipv4地址格式的字符串,如果主機(jī)名稱是ipv4地址,則它將保持不變
print("正在 Ping {0} [{1}] 具有 32 字節(jié)的數(shù)據(jù):".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} 的回復(fù): 字節(jié)=32 時(shí)間={1}ms".format(addr,int(times*1000)))
time.sleep(0.7)
else:
print("請(qǐng)求超時(shí)。")
if __name__ == "__main__":
if len(sys.argv) < 2:
sys.exit('Usage: ping.py <host>')
ping(sys.argv[1])
>python .\Ping_tool.py www.csdn.net 正在 Ping www.csdn.net [47.95.164.112] 具有 32 字節(jié)的數(shù)據(jù): 來自 47.95.164.112 的回復(fù): 字節(jié)=32 時(shí)間=39ms 來自 47.95.164.112 的回復(fù): 字節(jié)=32 時(shí)間=40ms 來自 47.95.164.112 的回復(fù): 字節(jié)=32 時(shí)間=40ms 來自 47.95.164.112 的回復(fù): 字節(jié)=32 時(shí)間=39ms
引用:
https://docs.python.org/3/library/socket.html
https://docs.python.org/3/library/select.html#select.select
https://docs.python.org/3/library/struct.html
以上這篇python實(shí)現(xiàn)一個(gè)簡單的ping工具方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python利用WMI實(shí)現(xiàn)ping命令的例子
- 在Python中調(diào)用Ping命令,批量IP的方法
- python 實(shí)現(xiàn)ping測(cè)試延遲的兩種方法
- Python 多線程C段掃描、檢測(cè) Ping掃描腳本的實(shí)現(xiàn)
- 使用Python實(shí)現(xiàn)批量ping操作方法
- 使用Python測(cè)試Ping主機(jī)IP和某端口是否開放的實(shí)例
- python實(shí)現(xiàn)本地批量ping多個(gè)IP的方法示例
- Python中typing模塊與類型注解的使用方法
- Python測(cè)試網(wǎng)絡(luò)連通性示例【基于ping】
- python實(shí)現(xiàn)ping命令小程序
相關(guān)文章
Python使用Opencv實(shí)現(xiàn)圖像特征檢測(cè)與匹配的方法
這篇文章主要介紹了Python使用Opencv實(shí)現(xiàn)圖像特征檢測(cè)與匹配的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Python串口通信的接收與發(fā)送的實(shí)現(xiàn)
串口通信是指通過串口進(jìn)行數(shù)據(jù)傳輸?shù)囊环N通信方式,本文就來介紹一下Python串口通信的接收與發(fā)送的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11
python中not not x 與bool(x) 的區(qū)別
這篇文章主要介紹了python中not not x 與 bool(x) 的區(qū)別,我們就來做一個(gè)選擇,就是 not not x 和 bool(x) 用哪個(gè)比較好?下面一起進(jìn)入文章看看吧2021-12-12
Python計(jì)算三角函數(shù)之a(chǎn)sin()方法的使用
這篇文章主要介紹了Python計(jì)算三角函數(shù)之a(chǎn)sin()方法的使用,是Python入門的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05
python繪制評(píng)估優(yōu)化算法性能的測(cè)試函數(shù)
這篇文章主要為大家詳細(xì)介紹了python繪制評(píng)估優(yōu)化算法性能的測(cè)試函數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
python網(wǎng)絡(luò)爬蟲實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼的方法
這篇文章主要介紹了python網(wǎng)絡(luò)爬蟲實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02

