Python socket.error:[Errno 13]权限被拒绝 [英] Python socket.error: [Errno 13] Permission denied

查看:1213
本文介绍了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))

我在根目录下启动此脚本并得到以下错误:

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套接字)试图发送到网络/广播地址,就好像它是单播地址一样.

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

如果您实际上想发送到广播地址,请设置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]权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆