Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help code found on web
#1
Hi All

Please could i ge tsome help as to why i am getting error with this code ?? I am new to python so please excuss my not understanding this error

Error:
Traceback (most recent call last): File "c:/Python Code/CSL Zoom Screen Control v1.1.py", line 32, in <module> wake_on_lan('0F:0F:DF:0F:BF:EF') File "c:/Python Code/CSL Zoom Screen Control v1.1.py", line 22, in wake_on_lan send_data = ''.join([send_data,struct.pack('B', int(data[i: i + 2], 16))]) TypeError: sequence item 1: expected str instance, bytes found
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 = ''.join(['FFFFFFFFFFFF', macaddress * 20])
    send_data = '' 

    # Split up the hex values and pack.
    for i in range(0, len(data), 2):
        send_data = ''.join([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, ('<broadcast>', 7))
    

if __name__ == '__main__':
    # Use macaddresses with any seperators.
    wake_on_lan('0F:0F:DF:0F:BF:EF')
    wake_on_lan('0F-0F-DF-0F-BF-EF')
    # or without any seperators.
    wake_on_lan('0F0FDF0FBFEF')
Reply
#2
You are probably getting this error because this code was written for python 2 instead of python 3. You could perhaps translate it with the 2to3 tool that ships with python.
Reply
#3
Hi

Thanks for you reply have tried that but still get same error, am losing the little hair that I have left !! Huh
Reply
#4
Add b two places,to make it work for Python 3.
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 = ''.join(['FFFFFFFFFFFF', macaddress * 20])
    send_data = b''

    # Split up the hex values and pack.
    for i in range(0, len(data), 2):
        send_data = b''.join([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, ('<broadcast>', 7))

if __name__ == '__main__':
    # Use macaddresses with any seperators.
    wake_on_lan('0F:0F:DF:0F:BF:EF')
    wake_on_lan('0F-0F-DF-0F-BF-EF')
    # or without any seperators.
    wake_on_lan('0F0FDF0FBFEF')
Reply
#5
This should work. I can't test it atm.


import binascii
import socket


def strip_mac_seperators(macaddress):
    """
    Strip : and - from macaddress and return it
    """
    return macaddress.replace("-", "").replace(":", "")


def mac2bytes(macaddress):
    """
    Generate the broadcast data from the macaddress.
    """
    data = "FF" * 6
    data += macaddress
    return binascii.unhexlify(data)


def wake_on_lan(macaddress):
    """
    Switches on remote computers using WOL.
    """
    macaddress = strip_mac_seperators(macaddress)
    if len(macaddress) != 12:
        raise ValueError("It's not a macaddress")

    try:
        data = mac2bytes(macaddress)
    except ValueError:
        raise ValueError("The mac address contains non hexadecimal characters.")

    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        sock.sendto(data, ("<broadcast>", 7))


if __name__ == "__main__":
    # Use macaddresses with any seperators.
    wake_on_lan("0F:0F:DF:0F:BF:EF")
    wake_on_lan("0F-0F-DF-0F-BF-EF")
    # or without any seperators.
    wake_on_lan("0F0FDF0FBFEF")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No Module found in other directory than source code [SOLVED] AlphaInc 1 2,004 Nov-10-2021, 04:34 PM
Last Post: AlphaInc
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 2 2,505 Nov-23-2020, 05:15 PM
Last Post: cananb
  How to fix error code 2 in python, “directory not found”? dav3javu 1 6,984 Apr-03-2019, 04:55 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020