本文介绍了Python socket.error: [Errno 13] 权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 Linux 和 Python,我想通过广播发送一些数据:
Using Linux and Python, I want to send some data with broadcast:
d = b'109u433279423423423'
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(d, 0, ('192.168.0.255', 9))
我在 root 下启动这个脚本并得到这个错误:
I launch this script under root and get this error:
s.sendto(d, 0, ('192.168.0.255', 9)) socket.error: [Errno 13]
Permission denied
怎么了?
推荐答案
您正在尝试发送到广播地址.这是不允许的,请参阅 sendto(2) 的手册页:
You are trying to send to a broadcast address. It is not allowed, see manpage for sendto(2):
EACCES(对于 UDP 套接字)尝试发送到网络/广播地址,就好像它是单播地址一样.
设置 SO_BROADCAST 选项,如果您实际上是要发送到广播地址:
Set the SO_BROADCAST option, if you actually mean to send to a broadcast address:
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
这篇关于Python socket.error: [Errno 13] 权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!