计算机网络相关题目及答案(第五章实验)

news/2024/7/27 14:03:17

实验:套接字编程作业5:ICMP ping

源代码如下:

# -*- coding: utf-8 -*-
"""
SAFA_LIYT
"""
import socket
import os
import sys
import struct
import time
import select
import binasciiICMP_ECHO_REQUEST = 8# 计算checksum
def checksum(str):csum = 0countTo = (len(str) / 2) * 2count = 0while count < countTo:thisVal = str[count+1] * 256 + str[count]csum = csum + thisValcsum = csum & 0xffffffffcount = count + 2if countTo < len(str):csum = csum + str[len(str) - 1].decode()csum = csum & 0xffffffffcsum = (csum >> 16) + (csum & 0xffff)csum = csum + (csum >> 16)answer = ~csumanswer = answer & 0xffffanswer = answer >> 8 | (answer << 8 & 0xff00)return answer# 客户机接收服务器的Pong响应
def receiveOnePing(mySocket, ID, sequence, destAddr, timeout):timeLeft = timeoutwhile 1:startedSelect = time.time()whatReady = select.select([mySocket], [], [], timeLeft)howLongInSelect = (time.time() - startedSelect)if whatReady[0] == []: # Timeoutreturn "Request timed out."timeReceived = time.time()recPacket, addr = mySocket.recvfrom(1024)#Fetch the ICMP header from the IP packet#获得ICMP_ECHO_REPLY结构体,取出校验和checksum、序列号ID、生存时间TTL#获得ICMP_ECHO_REPLY结构体,取出校验和checksum、序列号ID、生存时间TTLheader = recPacket[20: 28]type,code,checksum,packetID,sequence = struct.unpack("!bbHHh", header)if type == 0 and packetID == ID:byte_in_double = struct.calcsize("!d")timeSent = struct.unpack("!d", recPacket[28: 28 + byte_in_double])[0]delay = timeReceived - timeSentttl = ord(struct.unpack("!c", recPacket[8:9])[0].decode())return (delay, ttl, byte_in_double)timeLeft = timeLeft - howLongInSelectif timeLeft <= 0:return "Request timed out."# 客户机向服务器发送一个Ping报文
def sendOnePing(mySocket, ID, sequence, destAddr):# Header is type (8), code (8), checksum (16), id (16), sequence (16)myChecksum = 0# Make a dummy header with a 0 checksum.# struct -- Interpret strings as packed binary dataheader = struct.pack("!bbHHh", ICMP_ECHO_REQUEST, 0, myChecksum, ID, sequence)data = struct.pack("!d", time.time())# Calculate the checksum on the data and the dummy header.myChecksum = checksum(header + data)# Get the right checksum, and put in the header#if sys.platform == 'darwin':#    myChecksum = socket.htons(myChecksum) & 0xffff#    Convert 16-bit integers from host to network byte order.#else:#    myChecksum = socket.htons(myChecksum)header = struct.pack("!bbHHh", ICMP_ECHO_REQUEST, 0, myChecksum, ID, sequence)packet = header + data# AF_INET address must be tuple, not str#Both LISTS and TUPLES consist of a number of objects#which can be referenced by their position number within the objectmySocket.sendto(packet, (destAddr, 1)) 
# 客户机发出一次Ping请求
def doOnePing(destAddr, ID, sequence, timeout):icmp = socket.getprotobyname("icmp")#SOCK_RAW is a powerful socket type. For more details see: http://sock-raw.org/papers/sock_raw#SOCK_RAW:原始套接字#普通的套接字无法处理**ICMP**、IGMP等网络报文,而**SOCK_RAW**可以;#Create Socket here#创建一个套接字,使得客户机和服务器相关联mySocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)# 向服务器发送一个Ping报文sendOnePing(mySocket, ID, sequence, destAddr)# 从服务器接收一个Pong报文delay = receiveOnePing(mySocket, ID, sequence, destAddr, timeout)mySocket.close()return delay# 要调用的主体程序
def ping(host, timeout=1):# timeout=1 means: If one second goes by without a reply from the server,# the client assumes that either the client’s ping or the server’s pong is lostdest = socket.gethostbyname(host)print("Pinging " + dest + " using Python:")print("")# Send ping requests to a server separated by approximately one secondmyID = os.getpid() & 0xFFFFloss = 0for i in range(10): # 向服务器发出4次Ping请求result = doOnePing(dest, myID, i, timeout)if not result:print("第"+str(i)+"次Ping请求超时(>1s)")# 响应超时,丢包数量+1loss += 1else:delay = int(result[0]*1000)ttl = result[1]bytes = result[2]print("第"+str(i)+"次Ping请求成功:")print("收到的Pong响应消息:"+dest+":byte(s)="+str(bytes)+"delay="+str(delay)+"ms TTL="+str(ttl))time.sleep(1)print("发送次数:"+str(10)+" 发送成功次数:"+str(10-loss)+" 丢包次数:"+str(loss))return 
ping("www.baidu.com")

最短路径

步骤

N

D(t)p(t)

D(u)p(u)

D(v)p(v)

D(w)p(w)

D(y)p(y)

D(z)p(z)

0

X

--

--

3,x

6,x

6,x

--

1

Xv

7,v

6,v

3,x

6,x

4,v

--

2

Xvy

7,v

6,v

3,x

6,x

4,v

18,y

3

Xvyu

7,v

6,v

3,x

6,x

4,v

18,y

4

Xvyut

7,v

6,v

3,x

6,x

4,v

12,t

5

Xvyut

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.cpky.cn/p/7884.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

【Web】Spring rce CVE-2022-22965漏洞复现学习笔记

目录 原理概览 漏洞简述 Tomcat AccessLogValve 和 access_log 例题: 原理概览 spring框架在传参的时候会与对应实体类自动参数绑定&#xff0c;通过“.”还可以访问对应实体类的引用类型变量。使用getClass方法&#xff0c;通过反射机制最终获取tomcat的日志配置成员属性…

【Axure高保真原型】计算日期区间的天数差

今天和大家分享计算日期区间的天数差的原型模板&#xff0c;选择开始日期和结束日期&#xff0c;点击等于按钮后&#xff0c;就可以计算出这两个日期之间相差了多少天&#xff0c;本案例提供中继器版的日期选择器&#xff0c;以及JS版的日期选择器&#xff0c;具体效果可以观看…

Xcode配置GLFW GLAD (MAC)

这里的GLFW用的是静态链接 博主反复修改&#xff0c;实在是没能找到为什么用动态会出现线程报错 下载GLAD:版本我一般是选倒数第二新&#xff0c;profile记得选core 点击GENRATE 点glad.zip获得下载 下载GLFW 点击download 最后&#xff0c;将两个文件都放到项目里面去 打开…

小程序 自定义组件和生命周期

文章目录 ⾃定义组件创建⾃定义组件声明组件编辑组件注册组件 声明引⼊⾃定义组件⻚⾯中使⽤⾃定义组件定义段与⽰例⽅法组件-⾃定义组件传参过程 小程序生命周期应用生命周期页面生命周期页面生命周期 ⾃定义组件 类似vue或者react中的自定义组件 ⼩程序允许我们使⽤⾃定义组件…

T-Sql 也能更新修改查询JSON?

今天看见一个澳洲项目里面使用了 JSON_VALUE 这样的函数解析 JSON 我倍感诧异&#xff0c;我印象当中Sql Server并不支持JOSN的相关操作&#xff0c;他最多只把JSON当成一个字符串来存储&#xff0c;更不要说去解析&#xff0c;查询和更新了 我随后查询了下此函数&#xff0c;…

【蓝桥杯冲冲冲】Prime Gift

【蓝桥杯冲冲冲】Prime Gift 蓝桥杯备赛 | 洛谷做题打卡day31 文章目录 蓝桥杯备赛 | 洛谷做题打卡day31Prime Gift题面翻译题目描述输入格式输出格式样例 #1样例输入 #1样例输出 #1 样例 #2样例输入 #2样例输出 #2 提示题解代码我的一些话 Prime Gift 题面翻译 给你 n n n 个…