尝试使用 Raspberry Pi 4 作为网络上其他设备的网络唤醒集线器。以下脚本可通过 Windows 和 Ubuntu Server 18.04 工作,但不能通过 Raspian Buster 工作。这是利用wakeonlan模块。
from wakeonlan import send_magic_packet;
def wakeup():
send_magic_packet('_MAC_', ip_address='_subnet_.255', port=9)
if __name__ == "__main__":
wakeup()
在 Raspbian 上使用时,会出现权限错误:
Traceback (most recent call last):
File "foo.py", line 7, in <module>
wakeup()
File "foo.py", line 4, in wakeup
send_magic_packet('_MAC_', ip_address='_subnet_.255', port=9)
File "/usr/local/lib/python3.7/dist-packages/wakeonlan.py", line 58, in send_magic_packet
sock.send(packet)
PermissionError: [Errno 1] Operation not permitted
如您所见,这是通过 Python 3 运行的,我尝试运行脚本并sudo
获得相同的结果。通过提升的 Python 3 shell 运行它不会产生错误,但也不会产生任何结果。
这是错误import
中wakeonlan.py
引用的
import argparse
import socket
from typing import List
以及有问题的函数,即第 36 到 58 行。
def send_magic_packet(
*macs: str, ip_address: str = BROADCAST_IP, port: int = DEFAULT_PORT
) -> None:
"""
Wake up computers having any of the given mac addresses.
Wake on lan must be enabled on the host device.
Args:
macs: One or more macaddresses of machines to wake.
Keyword Args:
ip_address: the ip address of the host to send the magic packet to.
port: the port of the host to send the magic packet to.
"""
packets = [create_magic_packet(mac) for mac in macs]
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.connect((ip_address, port))
for packet in packets:
sock.send(packet)
两者都是从上面链接的 Github 存储库中提取的。
我不确定如何解决,因为它适用于 Ubuntu Server 和 Windows。任何意见,将不胜感激。我已经阅读了 Python 的潜在权限问题socket
,但找不到任何似乎适用于该问题的具体内容。