gpt4 book ai didi

python - Python 中的 UDP 客户端/服务器套接字

转载 作者:太空狗 更新时间:2023-10-29 17:39:39 25 4
gpt4 key购买 nike

我是 python 和套接字的新手,正在尝试编写一个回显客户端/服务器套接字。我已经编写了服务器,以便丢失 30% 的数据包。我将我的客户端编程为在一秒后超时,因为数据包可能会丢失。但是,每当我运行我的客户端套接字时,我的输出都是 100% REQUEST TIMED OUT。我假设我得到这个输出是因为我的服务器从未收到消息。我已经多次查看我的代码,但无法弄清楚为什么我会不断收到此输出。下面是我的服务器和客户端套接字代码。任何帮助,将不胜感激。

服务器套接字:

 # We will need the following module to generate randomized lost packets
import random
from socket import *

# 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
serverSocket.bind(('', 12000))

while True:
# Generate random number in the range of 0 to 10
rand = random.randint(0, 10)

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

# If rand is less is than 4, we consider the packet lost and do notrespond
if rand < 4:
continue

# Otherwise, the server responds
serverSocket.sendto(message, address)

客户端套接字:

import time
from socket import *

pings = 1

#Send ping 10 times
while pings < 11:

#Create a UDP socket
clientSocket = socket(AF_INET, SOCK_DGRAM)

#Set a timeout value of 1 second
clientSocket.settimeout(1)

#Ping to server
message = 'test'

addr = ("127.0.0.1", 12000)

#Send ping
start = time.time()
clientSocket.sendto(message, addr)

#If data is received back from server, print
try:
data, server = clientSocket.recvfrom(1024)
end = time.time()
elapsed = end - start
print data + " " + pings + " "+ elapsed

#If data is not received back from server, print it has timed out
except timeout:
print 'REQUEST TIMED OUT'

pings = pings - 1

最佳答案

我测试了您的代码,它在我的机器上按预期工作。您的问题可能不是您的代码。它可能是防火墙或其他阻止环回接口(interface) (127.0.0.1) 上所有数据包的东西。根据您的操作系统,尝试使用 Wireshark 等数据包监视器进行测试。

此外,这里有一些关于如何改进代码以使其更符合 Pythonic 的建议:

服务器

import random
import socket

server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(('', 12000))

while True:
rand = random.randint(0, 10)
message, address = server_socket.recvfrom(1024)
message = message.upper()
if rand >= 4:
server_socket.sendto(message, address)

客户端

import time
import socket

for pings in range(10):
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.settimeout(1.0)
message = b'test'
addr = ("127.0.0.1", 12000)

start = time.time()
client_socket.sendto(message, addr)
try:
data, server = client_socket.recvfrom(1024)
end = time.time()
elapsed = end - start
print(f'{data} {pings} {elapsed}')
except socket.timeout:
print('REQUEST TIMED OUT')

关于python - Python 中的 UDP 客户端/服务器套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27893804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com