Linux Python socket.error: [Errno 13] 权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11457676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:47:48  来源:igfitidea点击:

Python socket.error: [Errno 13] Permission denied

linuxpython-3.xudpdatagram

提问by atomAltera

Using Linux and Python, I want to send some data with broadcast:

使用 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))

I launch this script under root and get this error:

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

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

What is wrong?

怎么了?

采纳答案by fork0

You are trying to send to a broadcast address. It is not allowed, see manpage for sendto(2):

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

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

EACCES(对于 UDP 套接字)尝试发送到网络/广播地址,就好像它是单播地址一样。

Set the SO_BROADCAST option, if you actually mean to send to a broadcast address:

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

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