oscserver
0. 安装Python3.5以上版本
sudo apt-get install python3
0. 安装python-osc-1.7.2
pip install python-osc
import socket import binascii import pprint import time from pythonosc.udp_client import SimpleUDPClient # 导入客户端 from pythonosc.dispatcher import Dispatcher # 导入包中的文件dispatcher中的Dispatcher类 from pythonosc.osc_server import BlockingOSCUDPServer # 导入包中的osc_server中的BlockingOSCUDPSer$ WakeOnLan = '/wakeonlan' # 定义收到的OSC是'/wakeonlan' # 定义端口 ip_localhost = '192.168.0.127' # 本机IP地址 port_localhost = 5005 # 本机接收端口 ip_remote = '192.168.0.146' # 被发送到的IP地址 port_remote = 1337 # 被发送到的端口 welcome = f'OSC 服务器运行在本机的 [ {port_localhost} ] 端口......' print(welcome) # 收到'/wakeonlan' def print_handler(address,*args): print(f'收到唤醒魔包:{address}: {args}') # 发送魔包 ''' 格式化mac地址,生成魔包,然后发送 mac.txt 中的地址必须格式化为每行一个mac地址.如下形式都可以: 44850004F4EE 00-FF-AC-C0-BB-CA 44-85-00-04-F4-EE 44:87:01:04:F4:EE ''' f = lambda x:x.strip() if len(x.strip()) == 12 else x.strip().replace(x.strip()[2],'') mac = [f(r) for r in open('mac.txt')] print('-' * 24,'目标MAC地址列表','-' * 25) # mac.txt中的mac地址会被处理成FFFFFFFFFFFF无分隔符紧$ pprint.pprint(mac) print('-' * 66) ip = '192.168.0.146' # 0网段广播 port = 9 # 魔包发送端口 pswd = 'tocentek' # password pswd = pswd.encode() s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1) def sendto(r): s.sendto(r,(ip,port)) time.sleep(0.5) #python利用or在列表解析中调用多个函数 http://www.cnblogs.com/gayhub/p/5277919.html # 带密码 [print('向MAC地址:',r,'发送唤醒魔术包...') or sendto(binascii.unhexlify('FF'*6+r*16) + pswd ) f$ # 不带密码 # [print('向MAC地址:',r,'发送唤醒魔术包...') or sendto(binascii.unhexlify('FF'*6+r*16)) for r i$ s.close() print('-' * 15,'结束唤醒进程,如未开机,尝试再发一次','-' * 15) print('唤醒结束,继续:' , welcome) # ######################################## def default_handler(address,*args): client = SimpleUDPClient(ip_remote , port_remote) # 创建转发客户端 client.send_message(address,args) print('->' * 10 ,f'向 {ip_remote}:{port_remote} 转发: {address}: {args}','->' * 10 ) print('默认转发,继续:' , welcome) dispatcher = Dispatcher() dispatcher.map(WakeOnLan,print_handler) # 收到唤醒魔包,调用print_handler函数 dispatcher.set_default_handler(default_handler) # 否则直接转发 server = BlockingOSCUDPServer((ip_localhost , port_localhost) , dispatcher) # 创建服务器 server.serve_forever() # 一直跑
需要定义的参数:
1. 本地IP和端口:
ip_localhost = '192.168.0.152' # 本机IP port_localhost = 5005 # 本机接收端口
2. 远程IP和端口:
ip_remote = '192.168.0.195' # 被发送到的ip port_remote = 7008 # 远程端口
3. 魔包地址:
WakeOnLan = '/wakeonlan' # 定义收到的OSC是'/wakeonlan'
4. mac.txt 地址簿(程序目录下):
44850004F4EE 00-FF-AC-C0-BB-CA 55-85-00-04-E5-AB 66:87:01:02:F4:0D
本程序魔包发送部分引用自codegay,感谢大佬!