oscserver

import socket

import binascii
import pprint

from pythonosc.udp_client import SimpleUDPClient # Import client
from pythonosc.dispatcher import Dispatcher # Import the Dispatcher class in the dispatcher file in the package
from pythonosc.osc_server import BlockingOSCUDPServer # Import the BlockingOSCUDPServer class in osc_server in the package

WakeOnLan
= '/wakeonlan' # Define the received OSC is'/wakeonlan '
#
Define port
ip_localhost = '127.0.0.1'
port_localhost
= 5005 # Local receiving port

ip_remote
= '192.168.0.195' # The ip sent to
port_remote = 7008 # remote port

welcome
= f'OSC server runs on the local [{port_localhost }] Port...'
print(welcome)
# received ‘/wakeonlan’
def print_handler(address,*args):
print(f'Received Wake-up Magic Packet: {address}: {args}')
# Send magic package
‘‘‘
Format the mac address, generate a magic packet, and then send
The address in mac.txt must be formatted as one mac address per line. The following forms are all possible:
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,'Target MAC address list',' span>-' * 25) # The mac address in mac.txt will be processed into FFFFFFFFFFFF non-separator tight beating form
pprint.pprint(mac)
print('-' * 66)
ip
= '127.0.0.1' # 0 network segment broadcast
port = 9999 # Magic packet sending port
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))

#python uses or to call multiple functions in list analysis http: //www.cnblogs.com/gayhub/p/5277919.html
# With password
[print('For MAC address:',r,'Send a wake-up magic packet...') or sendto(binascii.unhexlify('FF'*6+r*16) + pswd) for r in mac]
# Without password
# [print('Send a wake-up magic packet to MAC address:',r,' ...') or sendto(binascii.unhexlify('FF'*6+r*16)) for r in mac]
s.close()
print('-' * 15,'End the wake-up process, if it is not turned on, try to send it again','-' * 15)
print(welcome)
# ############# ###########################

def default_handler(address,*args):
client
= SimpleUDPClient(ip_remote, port_remote) # Create a forwarding client< /span>
client.send_message(address,*args)
print('->' * 10 ,f'< span style="color: #800000;">Forward {ip_remote}:{port_remote}: {address}: {args}
', '->' span> * 10)
print(welcome)


dispatcher
= Dispatcher()
dispatcher.map(WakeOnLan,print_handler)
# Received Wakeup Magic Packet , Call the print_handler function
dispatcher.set_default_handler(default_handler) # otherwise directly forward

server
= BlockingOSCUDPServer((ip_localhost, port_localhost), dispatcher) # Create Server
server.serve_forever() # keep running

div>

Parameters to be defined:

  1. Local receiving port:

port_localhost = 5005 # Local receiving port

  2. Remote ip and port:

ip_remote = '192.168.0.195< span style="color: #800000;">' # The ip sent to 

port_remote = 7008 # remote port

  3. Magic bag address:

WakeOnLan = '/wakeonlan' # Define the received OSC is'/wakeonlan' 

  4. mac.txt address book (under the program directory):

< div class="code">

44850004F4EE

00-FF-AC-C0-BB-CA
55-85-00-04-E5-AB
66:87:01:02:F4:0D

The part of the magic package sent by this program is quoted from codegay, thank you guys !

import socket

import binascii
import pprint

from pythonosc.udp_client import SimpleUDPClient # Import client
from pythonosc.dispatcher import Dispatcher # Import the Dispatcher class in the dispatcher file in the package
from pythonosc.osc_server import BlockingOSCUDPServer # Import the BlockingOSCUDPServer class in osc_server in the package

WakeOnLan
= '/wakeonlan' # Define the received OSC is'/wakeonlan '
#
Define port
ip_localhost = '127.0.0.1'
port_localhost
= 5005 # Local receiving port

ip_remote
= '192.168.0.195' # The ip sent to
port_remote = 7008 # remote port

welcome
= f'OSC server runs on the local [{port_localhost }] Port...'
print(welcome)
# received ‘/wakeonlan’
def print_handler(address,*args):
print(f'Received Wake-up Magic Packet: {address}: {args}')
# Send magic package
‘‘‘
Format the mac address, generate a magic packet, and then send
The address in mac.txt must be formatted as one mac address per line. The following forms are all possible:
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,'Target MAC address list',' span>-' * 25) # The mac address in mac.txt will be processed into FFFFFFFFFFFF non-separator tight beating form
pprint.pprint(mac)
print('-' * 66)
ip
= '127.0.0.1' # 0 network segment broadcast
port = 9999 # Magic packet sending port
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))

#python uses or to call multiple functions in list analysis http: //www.cnblogs.com/gayhub/p/5277919.html
# With password
[print('For MAC address:',r,'Send a wake-up magic packet...') or sendto(binascii.unhexlify('FF'*6+r*16) + pswd) for r in mac]
# Without password
# [print('Send a wake-up magic packet to MAC address:',r,' ...') or sendto(binascii.unhexlify('FF'*6+r*16)) for r in mac]
s.close()
print('-' * 15,'End the wake-up process, if it is not turned on, try to send it again','-' * 15)
print(welcome)
# ############# ###########################

def default_handler(address,*args):
client
= SimpleUDPClient(ip_remote, port_remote) # Create a forwarding client< /span>
client.send_message(address,*args)
print('->' * 10 ,f'< span style="color: #800000;">Forward {ip_remote}:{port_remote}: {address}: {args}
', '->' span> * 10)
print(welcome)


dispatcher
= Dispatcher()
dispatcher.map(WakeOnLan,print_handler)
# Received Wakeup Magic Packet , Call the print_handler function
dispatcher.set_default_handler(default_handler) # otherwise directly forward

server
= BlockingOSCUDPServer((ip_localhost, port_localhost), dispatcher) # Create Server
server.serve_forever() # keep running

p>

port_localhost = 5005 # Local receiving port pre>

ip_remote = '192.168.0.195 ' # was sent to ip

port_remote = 7008 # remote port

WakeOnLan = '/wakeonlan' # Define the received OSC as'/wakeonlan' < /span>

44850004F4EE

00-FF-AC-C0-BB-CA
55-85-00-04-E5-AB
66:87:01:02:F4:0D

Leave a Comment

Your email address will not be published.