linux - Python 套接字错误 : [Errno 13] Permission denied

标签 linux python-3.x udp datagram

使用 Linux 和 Python,我想通过广播发送一些数据:

d = b'109u433279423423423'    

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

s.sendto(d, 0, ('192.168.0.255', 9))

我在 root 下启动这个脚本并得到这个错误:

s.sendto(d, 0, ('192.168.0.255', 9)) socket.error: [Errno 13]
Permission denied

怎么了?

最佳答案

您正在尝试发送到一个广播地址。这是不允许的,请参阅 sendto(2) 的联机帮助页:

EACCES (For UDP sockets) An attempt was made to send to a network/broadcast address as though it was a unicast address.

设置 SO_BROADCAST 选项,如果你真的想发送到一个广播地址:

s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

关于linux - Python 套接字错误 : [Errno 13] Permission denied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11457676/

相关文章:

linux - 为什么 echo $JAVA_HOME 对于所有用户来说都不相同

c++ - 使用terminfo的终端颜色?

linux - 删除所有早于给定时间的文件并记录其操作的命令

python 类变量定义问题

python - 使用 PySide 将按字母顺序排列的项目添加到 QComboBox

在客户端中使用 C 编程中的 UDP 检查服务器何时上线

linux - VFS 如何知道要调用哪些底层文件系统函数?

python - 为什么在删除列表时 IPython 管理内存的方式与 CPython 不同?

java - 大数据文件的udp

android - 如何在 Android 中使用 UDP?