我正在一个项目中,我们必须创建一个服务器端心跳来检测50秒的空闲时间,然后服务器脚本超时。
# UDP_HbS.py
import socket
from socket import *
import sys
import time
argv = sys.argv
serverIPaddress = argv[1]
maxTimeOuts = 50
prevPacket = 0
timeouts = 0
# Create a UDP socket
# # Notice the use of SOCK_DGRAM for UDP packets
serverSocket = socket(AF_INET, SOCK_DGRAM)
# Assign IP address and port number to socket, use port 12000
serverSocket.bind((serverIPaddress,12000))
while timeouts < maxTimeOuts:
# Receive the client packet along with the address it is coming from
message, address = serverSocket.recvfrom(1024)
# Capitalize the message from the client
message = message.upper()
serverSocket.settimeout(1)
serverSocket.sendto(message, address)
packet = int(message)
if packet != prevPacket+1:
lost = packet-prevPacket-1
print('Lost ' + str(lost) + ' messages')
print('Server recieved msg ' + message)
if packet == prevPacket+1:
print('Server recieved msg ' + message)
try:
prevPacket = packet
except serverSocket.timeout:
timeouts+=1
print ('Server timeout' + str(timeouts),e )
print ('Connection to Client timed out after ' + timeouts + 'seconds.')
我收到错误消息“捕获不继承自Exception的异常:timeoutpylint(catching-non-exception)”,并且不确定如何纠正它。任何建议或帮助,将不胜感激。