日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python调用库实现返回ping的时延_python网络作业:使用python的socket库实现ICMP协议的ping...

發布時間:2023/12/31 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python调用库实现返回ping的时延_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

#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

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()

參考資料

執行

注意要有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调用库实现返回ping的时延_python网络作业:使用python的socket库实现ICMP协议的ping...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美色图11p| 日韩视频免费观看高清完整版在线观看 | 欧美一卡二卡在线 | 精品无码人妻一区二区三 | 超碰v | 成为性瘾网黄的yy对象后 | 午夜三级av | 在线观看国产成人 | 国产网红女主播精品视频 | 婷婷国产一区 | 高清毛片aaaaaaaaa片 | 亚洲天堂三区 | 久久久久香蕉视频 | 黄色网络在线观看 | 亚洲最大成人综合网 | 九九视频这里只有精品 | 国产传媒欧美日韩 | 亚洲欧美乱日韩乱国产 | 日韩爱爱网站 | 99国产精品久久久久久久 | 搡老岳熟女国产熟妇 | 日韩免费视频观看 | 亚洲成人av网址 | 国产精品久久久一区二区 | 日批视频免费观看 | 国产在线精品视频 | 久久久久麻豆v国产精华液好用吗 | www.猫咪av | 欧美变态口味重另类 | 毛片在线免费观看网址 | 色5月婷婷| 在线播放成人av | 天堂网中文 | 日本xx视频 | 91精品网站 | 欧美日韩国产传媒 | 欧美大黄视频 | 操操操免费视频 | 麻豆影视在线 | 91网站在线免费看 | 国产精品一区麻豆 | 国模私拍一区二区三区 | 秋霞一区二区 | 中文字幕免费视频观看 | 成人动漫在线观看免费 | 一区二区三区中文字幕在线观看 | 男人天堂网在线视频 | 亚洲久久一区二区 | 国产精品麻豆欧美日韩ww | 四虎成人精品在永久免费 | 中文一区二区在线 | 天天做天天爽 | 亚洲欧洲自拍偷拍 | gv天堂gv无码男同在线观看 | 吸咬奶头狂揉60分钟视频 | 国产欧美视频一区 | 亚洲黄色在线观看 | 亚洲综合在线中文字幕 | 巨大黑人极品videos精品 | 成人毛片在线免费观看 | av先锋资源| 中文无码熟妇人妻av在线 | 国产中文字字幕乱码无限 | 日本不卡一区二区三区 | 精品视频久久久久久久 | 欧美乱大交 | 好吊操视频这里只有精品 | 亚洲制服一区 | 老司机精品在线 | 玖玖热在线视频 | 一级片手机在线观看 | 婷婷成人av | 老司机在线永久免费观看 | 亚洲爱爱视频 | 国产视频中文字幕 | 综合视频一区 | 国产精品久久久影院 | 亚洲AV无码精品色毛片浪潮 | 一级视频片 | 亚洲精品一区二区三区四区乱码 | 欧美人体做爰大胆视频 | 国产伦精品一区二区三区视频痴汉 | 日韩精品不卡 | 99国产精品欲 | 欧美成人三级视频 | 五月激情久久 | 泰剧19禁啪啪无遮挡 | 国内一级黄色片 | 免费成人激情视频 | 国产成人在线播放 | 绿帽视频| 成熟人妻av无码专区 | 日本少妇裸体做爰高潮片 | 9i看片成人免费看片 | 玖玖玖在线观看 | 亚洲尤物视频 | 秋霞成人午夜伦在线观看 | 亚洲天天看 | 中国美女一级黄色片 |