日韩性视频-久久久蜜桃-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...的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 精品国产露脸精彩对白 | 欧美日韩一区二区中文字幕 | 色姐| 国产一区二区网 | 欧美中文字幕一区二区 | 日本免费专区 | 一直草| 亚洲久久在线观看 | 久久精品欧美一区二区三区麻豆 | 99资源在线 | 亚洲综合免费观看高清完整版在线 | 一本久久综合亚洲鲁鲁五月天 | 91精品国产综合久久久久 | 麻豆爱爱 | 婷婷开心激情 | 日本aa大片| 国产熟女一区二区 | 日本啊啊视频 | www.五月婷婷 | 久久久久久99精品久久久 | 免费毛片一级 | 麻豆传媒一区二区三区 | 亚洲熟女乱综合一区二区 | 天天想你在线观看完整版电影免费 | 免费日韩毛片 | 吸咬奶头狂揉60分钟视频 | 国产理论在线观看 | 中文日本在线 | 麻豆www| 亚洲精品国产精品乱码不66 | 色呦呦在线免费观看 | 国产三级视频在线 | 国产人妻精品午夜福利免费 | 欧美日韩99 | 放荡的美妇在线播放 | 亚洲视频手机在线观看 | 看91| 黑人高潮一区二区三区在线看 | 滋润少妇h高h | 国产a级精品 | 中文在线不卡视频 | 人妻熟女一区二区aⅴ水 | 亚洲精品中文字幕在线播放 | 少妇又紧又色又爽又刺激 | 91九色porny视频 | 免费无码肉片在线观看 | 自拍视频网站 | 久久亚洲天堂 | 17c在线观看 | 999在线观看视频 | 日韩色道 | 在线爱情大片免费观看大全 | 夜夜爽妓女8888视频免费观看 | 嫩草影院永久入口 | 岛国精品一区二区三区 | 免费观看视频一区 | avtt中文字幕| 亚洲av熟女高潮一区二区 | a免费在线| 99热日本 | 激情综合图区 | 中文字幕一区二区在线老色批影视 | 理论片在线观看视频 | 日本激情视频一区二区三区 | 一起草av在线 | 人人妻人人澡人人爽人人精品 | 国产亚洲精品女人久久久久久 | 污视频网址在线观看 | 欧美一区二区在线看 | 九色porny自拍 | av不卡免费观看 | 欧美日韩精品一区二区三区四区 | 黄色一级片在线免费观看 | 日本一级网站 | 亚洲精品xxxx | 一区二区一级片 | 91一级片 | 国产精品九色 | 中文字幕在线播放视频 | 欧美日韩国产综合在线 | 国产小视频自拍 | 精品国产aⅴ一区二区三区东京热 | 91爱啪啪 | а√天堂www在线天堂小说 | 中文字幕亚洲一区二区三区 | 欧美aa| 久久久精品人妻av一区二区三区 | 精品在线视频一区二区三区 | 国产精品一级无码 | 国产成人无码精品久久久性色 | 国产精品1234区 | 成人性生交视频免费观看 | 91人人澡人人爽 | 亚洲精品少妇一区二区 | fc2成人免费视频 | 久久久国产精品人人片 | 日本视频在线观看免费 | jizz亚洲女人高潮大叫 | 日韩av免费看 |