您好,登錄后才能下訂單哦!
繼上一篇計算checksum校驗和,本章通過socket套接字,struct字節打包成二進制,select返回套接字的文件描述符的結合,實現一個簡單的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): """ 校驗 """ 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(sys.argv[1])
>python .\Ping_tool.py www.csdn.net 正在 Ping www.csdn.net [47.95.164.112] 具有 32 字節的數據: 來自 47.95.164.112 的回復: 字節=32 時間=39ms 來自 47.95.164.112 的回復: 字節=32 時間=40ms 來自 47.95.164.112 的回復: 字節=32 時間=40ms 來自 47.95.164.112 的回復: 字節=32 時間=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實現一個簡單的ping工具方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。