Python:RPI上的简单UDP服务器不打印消息

时间:2017-01-11 09:55:49

标签: python sockets udp

我在覆盆子pi上有一个简单的UDP服务器,ipv6地址为“2a02:2c40:100:a001:1de0:4ff6:5bbd:5cd9”

udp服务器的代码是:

import socket
import sys

# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind the socket to the port
server_address = ('localhost', 61625)
print('starting up on {} port {}'.format(*server_address))
sock.bind(server_address)

while True:
    print('\nwaiting to receive message')
    data, address = sock.recvfrom(4096)

    print('received {} bytes from {}'.format(
        len(data), address))
    print(data)

    if data:
        sent = sock.sendto(data, address)
        print('sent {} bytes back to {}'.format(
            sent, address))

在Windows机器上,我使用Packet Sender工具发送消息。 Wireshark将此消息捕获为:

80876   2260.986515 2a02:2c40:100:a001:d0cc:264f:f203:2ca0  2a02:2c40:100:a001:1de0:4ff6:5bbd:5cd9  UDP 69  61625→61625 Len=7

并立即使用此icmpv6消息:

80877   2260.987088 2a02:2c40:100:a001:1de0:4ff6:5bbd:5cd9  2a02:2c40:100:a001:d0cc:264f:f203:2ca0  ICMPv6  117 Destination Unreachable (Port unreachable)

关于RPI本身,我也看到了这一点:

388  10.578376 2a02:2c40:100:a001:d0cc:264f:f203:2ca0 -> 2a02:2c40:100:a001:1de0:4ff6:5bbd:5cd9 UDP 69 Source port: 61625  Destination port: 61625
389  10.578609 2a02:2c40:100:a001:1de0:4ff6:5bbd:5cd9 -> 2a02:2c40:100:a001:d0cc:264f:f203:2ca0 ICMPv6 117 Destination Unreachable (Port unreachable)

我的udp服务器没有打印任何内容,即仍然像这样:

pi@raspberrypi:~/git/cerberos_manager $ sudo python3 test_udp_server.py
starting up on localhost port 61625

waiting to receive message

我不知道我做错了什么。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

啊哈!修正了它。

使用IPv6,因此需要IPv6套接字。

sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)