import sys, socket, select, time
from argparse import ArgumentParser

XBOX_PORT = 5050
XBOX_PING = "dd00000a000000000000000400000002"
XBOX_POWER = "dd02001300000010"

py3 = sys.version_info[0] > 2


def main():
    parser = ArgumentParser(description="Send power on packets to a Xbox One.")
    parser.add_argument('-a', '--address', dest='ip_addr', help="IP Address of Xbox One", default='')
    parser.add_argument('-i', '--id', dest='live_id', help="Live ID of Xbox One", default='')
    parser.add_argument('-f', dest='forever', help="Send packets until Xbox is on", action='store_true')
    args = parser.parse_args()
        
    if not args.ip_addr:
        args.ip_addr = user_input("Enter the IP address: ")

    if not args.live_id:
        args.live_id = user_input("Enter the Live ID: ")

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setblocking(0)
    s.bind(("", 0))
    s.connect((args.ip_addr, XBOX_PORT))

    if isinstance(args.live_id, str):
        live_id = args.live_id.encode()
    else:
        live_id = args.live_id

    power_packet = bytearray.fromhex(XBOX_POWER) + live_id + b'\x00'
    print("Sending power on packets to {0}...".format(args.ip_addr))
    send_power(s, power_packet)

    print("Xbox should turn on now, pinging to make sure...")
    ping_result = send_ping(s)

    if ping_result:
        print("Ping successful!")
    else:
        print("Failed to ping Xbox :(")
        result = ""
        if not args.forever:
            while result not in ("y", "n"):
                result = user_input("Do you wish to keep trying? (y/n): ").lower()
        if args.forever or result == "y":
            print("Sending power packets and pinging until Xbox is on...")
            while not ping_result:
                send_power(s, power_packet)
                ping_result = send_ping(s)
            print("Ping successful!")

    s.close()


def send_power(s, data, times=5):
    for i in range(0, times):
        s.send(data)
        time.sleep(1)


def send_ping(s):
    s.send(bytearray.fromhex(XBOX_PING))
    return select.select([s], [], [], 5)[0]


def user_input(text):
    response = ""

    while response == "":
        if py3:
            response = input(text)
        else:
            response = raw_input(text)

    return response

if __name__ == "__main__":
    main()

@@ -130,40 +130,8 @@
 002%22
-%0AXBOX_POWER = %22dd02001300000010%22
 %0A%0Apy
@@ -1038,64 +1038,186 @@
 r_pa
-cket = bytearray.fromhex(XBOX_POWER) + live_id + b'%5Cx00'
+yload = b'%5Cx00' + chr(len(live_id)) + live_id + b'%5Cx00'%0A    power_header = b'%5Cxdd%5Cx02%5Cx00' + chr(len(power_payload)) + b'%5Cx00%5Cx00'%0A    power_packet = power_header + power_payload
 %0A   

Fix up the power on packet with correct lengths