Wake-On-Lan with Python 3.x

Example that works with python 3.x in ActiveState Code recipes collection:

 #!/usr/bin/env > # wol.py
  #
  # This module is from ActiveState Code Recipes:
  # and patched for Python 3 with:
  # http://code.activestate.com/recipes/577609-wake-on-lan-for-python-3/
  #
  # Example:
  # import wol
  # wol.wake_on_lan('70:F3:95:15:00:B5')
  #
import socket
import struct
  
def wake_on_lan(macaddress):
     """ Switches on remote computers using WOL. """
     
     # Check macaddress format and try to compensate
     if len (macaddress) = = 12 :
         pass
     elif len (macaddress) = = 12 + 5 :
         sep = macaddress[ 2 ]
         macaddress = macaddress.replace(sep,'')
     else :
         raise ValueError( 'Incorrect MAC address format' )
         
         
     # Pad the synchronization stream
     data = b 'FFFFFFFFFFFF' + (macaddress * 20 ).encode()
     send_data = b''
     
     # Split up the hex values in pack
     for i in range ( 0 , len (data), 2 ):
         send_data + = struct.pack( 'B' , int (data[i: i + 2 ], 16 ))
     
     # Broadcast it to the LAN
     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1 )
     sock.sendto(send_data, ( '255.255.255.255' , 7 ))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值