Python 将 UDP 套接字绑定到低端口

Python bind UDP Socket to low port

提问人:Alexander Wilhelm 提问时间:10/25/2023 更新时间:10/25/2023 访问量:21

问:

我想将 UDP 服务器绑定到低端口(例如端口 500),但我总是收到错误

[错误号 10013]试图以套接字访问权限禁止的方式访问套接字

我尝试使用管理员权限运行该程序,但出现相同的错误。

当我尝试使用TCP套接字时,它可以正常工作。此外,当我使用高端口 > 1023 时

这是我的代码:

def UDP_connect(ip, port_number, delay, output, source_ip='0', source_port=0):
    UDPsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    UDPsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    UDPsock.settimeout(delay)
    if source_ip != '0' and source_port > 0:
        UDPsock.bind((source_ip, source_port))

    try:
        data = "Hello"
        s.sendto(data,(ip,port_number))
        if len(s.recvfrom(1024)) > 0:
            output[port_number] = 'Listening'
        else:
            output[port_number] = ''
    except:
        output[port_number] = ''

    UDPsock.close()
Python 套接字 UDP 绑定

评论

0赞 user207421 10/25/2023
保留 UDP 端口号 500:“isakmp 500 udp isakmp”,相邻端口也是如此。你为什么要使用它们?

答: 暂无答案