当前位置:   article > 正文

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

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

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

源代码如下:

  1. # -*- coding: utf-8 -*-
  2. """
  3. SAFA_LIYT
  4. """
  5. import socket
  6. import os
  7. import sys
  8. import struct
  9. import time
  10. import select
  11. import binascii
  12. ICMP_ECHO_REQUEST = 8
  13. # 计算checksum
  14. def checksum(str):
  15. csum = 0
  16. countTo = (len(str) / 2) * 2
  17. count = 0
  18. while count < countTo:
  19. thisVal = str[count+1] * 256 + str[count]
  20. csum = csum + thisVal
  21. csum = csum & 0xffffffff
  22. count = count + 2
  23. if countTo < len(str):
  24. csum = csum + str[len(str) - 1].decode()
  25. csum = csum & 0xffffffff
  26. csum = (csum >> 16) + (csum & 0xffff)
  27. csum = csum + (csum >> 16)
  28. answer = ~csum
  29. answer = answer & 0xffff
  30. answer = answer >> 8 | (answer << 8 & 0xff00)
  31. return answer
  32. # 客户机接收服务器的Pong响应
  33. def receiveOnePing(mySocket, ID, sequence, destAddr, timeout):
  34. timeLeft = timeout
  35. while 1:
  36. startedSelect = time.time()
  37. whatReady = select.select([mySocket], [], [], timeLeft)
  38. howLongInSelect = (time.time() - startedSelect)
  39. if whatReady[0] == []: # Timeout
  40. return "Request timed out."
  41. timeReceived = time.time()
  42. recPacket, addr = mySocket.recvfrom(1024)
  43. #Fetch the ICMP header from the IP packet
  44. #获得ICMP_ECHO_REPLY结构体,取出校验和checksum、序列号ID、生存时间TTL
  45. #获得ICMP_ECHO_REPLY结构体,取出校验和checksum、序列号ID、生存时间TTL
  46. header = recPacket[20: 28]
  47. type,code,checksum,packetID,sequence = struct.unpack("!bbHHh", header)
  48. if type == 0 and packetID == ID:
  49. byte_in_double = struct.calcsize("!d")
  50. timeSent = struct.unpack("!d", recPacket[28: 28 + byte_in_double])[0]
  51. delay = timeReceived - timeSent
  52. ttl = ord(struct.unpack("!c", recPacket[8:9])[0].decode())
  53. return (delay, ttl, byte_in_double)
  54. timeLeft = timeLeft - howLongInSelect
  55. if timeLeft <= 0:
  56. return "Request timed out."
  57. # 客户机向服务器发送一个Ping报文
  58. def sendOnePing(mySocket, ID, sequence, destAddr):
  59. # Header is type (8), code (8), checksum (16), id (16), sequence (16)
  60. myChecksum = 0
  61. # Make a dummy header with a 0 checksum.
  62. # struct -- Interpret strings as packed binary data
  63. header = struct.pack("!bbHHh", ICMP_ECHO_REQUEST, 0, myChecksum, ID, sequence)
  64. data = struct.pack("!d", time.time())
  65. # Calculate the checksum on the data and the dummy header.
  66. myChecksum = checksum(header + data)
  67. # Get the right checksum, and put in the header
  68. #if sys.platform == 'darwin':
  69. # myChecksum = socket.htons(myChecksum) & 0xffff
  70. # Convert 16-bit integers from host to network byte order.
  71. #else:
  72. # myChecksum = socket.htons(myChecksum)
  73. header = struct.pack("!bbHHh", ICMP_ECHO_REQUEST, 0, myChecksum, ID, sequence)
  74. packet = header + data
  75. # AF_INET address must be tuple, not str
  76. #Both LISTS and TUPLES consist of a number of objects
  77. #which can be referenced by their position number within the object
  78. mySocket.sendto(packet, (destAddr, 1))
  79. # 客户机发出一次Ping请求
  80. def doOnePing(destAddr, ID, sequence, timeout):
  81. icmp = socket.getprotobyname("icmp")
  82. #SOCK_RAW is a powerful socket type. For more details see: http://sock-raw.org/papers/sock_raw
  83. #SOCK_RAW:原始套接字
  84. #普通的套接字无法处理**ICMP**、IGMP等网络报文,而**SOCK_RAW**可以;
  85. #Create Socket here
  86. #创建一个套接字,使得客户机和服务器相关联
  87. mySocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
  88. # 向服务器发送一个Ping报文
  89. sendOnePing(mySocket, ID, sequence, destAddr)
  90. # 从服务器接收一个Pong报文
  91. delay = receiveOnePing(mySocket, ID, sequence, destAddr, timeout)
  92. mySocket.close()
  93. return delay
  94. # 要调用的主体程序
  95. def ping(host, timeout=1):
  96. # timeout=1 means: If one second goes by without a reply from the server,
  97. # the client assumes that either the client’s ping or the server’s pong is lost
  98. dest = socket.gethostbyname(host)
  99. print("Pinging " + dest + " using Python:")
  100. print("")
  101. # Send ping requests to a server separated by approximately one second
  102. myID = os.getpid() & 0xFFFF
  103. loss = 0
  104. for i in range(10): # 向服务器发出4次Ping请求
  105. result = doOnePing(dest, myID, i, timeout)
  106. if not result:
  107. print("第"+str(i)+"次Ping请求超时(>1s)")# 响应超时,丢包数量+1
  108. loss += 1
  109. else:
  110. delay = int(result[0]*1000)
  111. ttl = result[1]
  112. bytes = result[2]
  113. print("第"+str(i)+"次Ping请求成功:")
  114. print("收到的Pong响应消息:"+dest+":byte(s)="+str(bytes)+"delay="+str(delay)+"ms TTL="+str(ttl))
  115. time.sleep(1)
  116. print("发送次数:"+str(10)+" 发送成功次数:"+str(10-loss)+" 丢包次数:"+str(loss))
  117. return
  118. 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

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号