import socket
# 创建UDP socket
udp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 绑定地址和端口
server_address = ('localhost', 12000)
udp_server_socket.bind(server_address)
# 服务器循环
while True:
# 接收客户端消息
message, client_address = udp_server_socket.recvfrom(1024)
print(f"Received message: {message} from {client_address}")
# 处理消息(这里仅回显相同的消息)
udp_server_socket.sendto(message, client_address)
# 关闭socket
udp_server_socket.close()
python实现UDP服务器
最新推荐文章于 2024-07-11 14:50:23 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)