[雪峰磁針石部落格]python網路作業:使用python的socket庫實現ICMP協議的ping
ICMP ping是您遇到過的最常見的網路掃描型別。 開啟命令列提示符或終端並輸入ping www.google.com非常容易。
為什麼要在python中實現?
- 很多名牌大學喜歡考試用python的socket庫實現ICMP協議的ping
- 個別環境沒有ping
直接上程式碼:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 技術支援:https://www.jianshu.com/u/69f40328d4f0
# 技術支援 https://china-testing.github.io/
# https://github.com/china-testing/python-api-tesing/blob/master/practices/ping.py
# 討論釘釘免費群21745728 qq群144081101 567351477
# CreateDate: 2018-11-22
import os
import argparse
import socket
import struct
import select
import time
ICMP_ECHO_REQUEST = 8 # Platform specific
DEFAULT_TIMEOUT = 2
DEFAULT_COUNT = 4
class Pinger(object):
""" Pings to a host -- the Pythonic way"""
def __init__(self, target_host, count=DEFAULT_COUNT, timeout=DEFAULT_TIMEOUT):
self.target_host = target_host
self.count = count
self.timeout = timeout
def do_checksum(self, source_string):
""" Verify the packet integritity """
sum = 0
max_count = (len(source_string)/2)*2
count = 0
while count < max_count:
val = source_string[count + 1]*256 + source_string[count]
sum = sum + val
sum = sum & 0xffffffff
count = count + 2
if max_count<len(source_string):
sum = sum + ord(source_string[len(source_string) - 1])
sum = sum & 0xffffffff
sum = (sum >> 16) + (sum & 0xffff)
sum = sum + (sum >> 16)
answer = ~sum
answer = answer & 0xffff
answer = answer >> 8 | (answer << 8 & 0xff00)
return answer
def receive_pong(self, sock, ID, timeout):
"""
Receive ping from the socket.
"""
time_remaining = timeout
while True:
start_time = time.time()
readable = select.select([sock], [], [], time_remaining)
time_spent = (time.time() - start_time)
if readable[0] == []: # Timeout
return
time_received = time.time()
recv_packet, addr = sock.recvfrom(1024)
icmp_header = recv_packet[20:28]
type, code, checksum, packet_ID, sequence = struct.unpack(
"bbHHh", icmp_header
)
if packet_ID == ID:
bytes_In_double = struct.calcsize("d")
time_sent = struct.unpack("d", recv_packet[28:28 + bytes_In_double])[0]
return time_received - time_sent
time_remaining = time_remaining - time_spent
if time_remaining <= 0:
return
def send_ping(self, sock, ID):
"""
Send ping to the target host
"""
target_addr = socket.gethostbyname(self.target_host)
my_checksum = 0
# Create a dummy heder with a 0 checksum.
header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1)
bytes_In_double = struct.calcsize("d")
data = (192 - bytes_In_double) * "Q"
data = struct.pack("d", time.time()) + bytes(data.encode(`utf-8`))
# Get the checksum on the data and the dummy header.
my_checksum = self.do_checksum(header + data)
header = struct.pack(
"bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), ID, 1
)
packet = header + data
sock.sendto(packet, (target_addr, 1))
def ping_once(self):
"""
Returns the delay (in seconds) or none on timeout.
"""
icmp = socket.getprotobyname("icmp")
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
except socket.error as e:
if e.errno == 1:
# Not superuser, so operation not permitted
e.msg += "ICMP messages can only be sent from root user processes"
raise socket.error(e.msg)
except Exception as e:
print ("Exception: %s" %(e))
my_ID = os.getpid() & 0xFFFF
self.send_ping(sock, my_ID)
delay = self.receive_pong(sock, my_ID, self.timeout)
sock.close()
return delay
def ping(self):
"""
Run the ping process
"""
for i in range(self.count):
print ("Ping to %s..." % self.target_host,)
try:
delay = self.ping_once()
except socket.gaierror as e:
print ("Ping failed. (socket error: `%s`)" % e[1])
break
if delay == None:
print ("Ping failed. (timeout within %ssec.)" % self.timeout)
else:
delay = delay * 1000
print ("Get pong in %0.4fms" % delay)
if __name__ == `__main__`:
parser = argparse.ArgumentParser(description=`Python ping`)
parser.add_argument(`host`, action="store", help=u`主機名`)
given_args = parser.parse_args()
target_host = given_args.host
pinger = Pinger(target_host=target_host)
pinger.ping()
參考資料
- 本文最新版本地址
- 討論 釘釘群21745728 qq群144081101 567351477
- 本文涉及的python測試開發庫 謝謝點贊!
- 本文相關海量書籍下載
- python 3.7極速入門教程9最佳python中文工具書籍下載
- 最新程式碼地址
執行
注意要有root或管理員許可權:
# python3 ping.py china-testing.github.io
Ping to china-testing.github.io...
Get pong in 160.7175ms
Ping to china-testing.github.io...
Get pong in 160.8465ms
Ping to china-testing.github.io...
Get pong in 12.0983ms
Ping to china-testing.github.io...
Get pong in 161.3324ms
# python3 ping.py www.so.com
Ping to www.so.com...
Get pong in 29.0303ms
Ping to www.so.com...
Get pong in 28.8599ms
Ping to www.so.com...
Get pong in 28.9860ms
Ping to www.so.com...
Get pong in 29.0167ms
相關文章
- [雪峰磁針石部落格]可愛的python測試開發庫Python
- [雪峰磁針石部落格]2019-Python最佳資料科學工具庫Python資料科學
- [雪峰磁針石部落格]pythontkinter圖形工具樣式作業Python
- [雪峰磁針石部落格]2018最佳python編輯器和IDEPythonIDE
- [雪峰磁針石部落格]python應用效能監控工具簡介Python
- [雪峰磁針石部落格]python爬蟲cookbook1爬蟲入門Python爬蟲
- [雪峰磁針石部落格]python庫介紹-argparse:命令列選項及引數解析Python命令列
- [雪峰磁針石部落格]python人工智慧作業:Windows使用SAPI和tkinter用不到40行實現文字轉語音工具Python人工智慧WindowsAPI
- [雪峰磁針石部落格]python包管理工具:Conda和pip比較Python
- [雪峰磁針石部落格]介面測試面試題面試題
- [雪峰磁針石部落格]tesseractOCR識別工具及pytesseract
- [雪峰磁針石部落格]pythonGUI作業:tkinter控制元件改變背景色PythonNGUI控制元件
- [雪峰磁針石部落格]使用python3和flask構建RESTfulAPI(介面測試服務)PythonFlaskRESTAPI
- [雪峰磁針石部落格]python計算機視覺深度學習1簡介Python計算機視覺深度學習
- [雪峰磁針石部落格]python標準模組介紹-string:文字常量和模板Python
- [雪峰磁針石部落格]multi-mechanize效能測試工具
- [雪峰磁針石部落格]python計算機視覺深度學習2影像基礎Python計算機視覺深度學習
- [雪峰磁針石部落格]大資料Hadoop工具python教程9-Luigi工作流大資料HadoopPythonUI
- [雪峰磁針石部落格]2018最佳ssh免費登陸工具
- [雪峰磁針石部落格]使用jython進行dubbo介面及ngrinder效能測試
- [雪峰磁針石部落格]pythonGUI工具書籍下載-持續更新PythonNGUI
- [雪峰磁針石部落格]資料倉儲快速入門教程1簡介
- [雪峰磁針石部落格]軟體自動化測試初學者忠告
- [雪峰磁針石部落格]Bokeh資料視覺化工具1快速入門視覺化
- [雪峰磁針石部落格]selenium自動化測試工具python筆試面試專案實戰5鍵盤操作Python筆試面試
- [雪峰磁針石部落格]Python經典面試題:用3種方法實現堆疊和佇列並示例實際應用場景Python面試題佇列
- [雪峰磁針石部落格]滲透測試簡介1滲透測試簡介
- [雪峰磁針石部落格]flask構建自動化測試平臺1-helloFlask
- [雪峰磁針石部落格]flask構建自動化測試平臺3-模板Flask
- Python使用socket的UDP協議實現FTP檔案服務PythonUDP協議FTP
- [雪峰磁針石部落格]web開發工具flask中文英文書籍下載-持續更新WebFlask
- [雪峰磁針石部落格]軟體測試專家工具包1web測試Web
- [雪峰磁針石部落格]python3.7極速入門教程1安裝:Linux(Ubuntu18.04)及Windows上安裝AnacondaPythonLinuxUbuntuWindows
- [雪峰磁針石部落格]資料分析工具pandas快速入門教程4-資料匯聚
- [雪峰磁針石部落格]計算機視覺opcencv工具深度學習快速實戰1人臉識別計算機視覺深度學習
- Linux C++ 實現一個簡易版的ping (也就是ICMP協議)LinuxC++協議
- [雪峰磁針石部落格]flask構建自動化測試平臺7-新增google地圖FlaskGo地圖
- [雪峰磁針石部落格]計算機視覺opcencv工具深度學習快速實戰2opencv快速入門計算機視覺深度學習OpenCV