Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError
#1
Hi

Please could someone help with the error I am getting with my code

Error:
Connected by ('127.0.0.1', 51995) b'PON' Command Screen On Received Traceback (most recent call last): File "c:/Python Code/new test.py", line 55, in <module> server() File "c:/Python Code/new test.py", line 43, in server udp.sendto(packet_to_sned, (Screen_ip_address, UDP_PORT)) TypeError: a bytes-like object is required, not 'str'
s.listen(1)

filepath = 'config.cfg'
with open(filepath) as fp:
   line = fp.readline()
   cnt = 1
   while line:
       if cnt == 1:
           Screen_mac_address = int(line,16)
           print(hex(Screen_mac_address))
       if cnt == 2:
           ip_address = line 
           print(ip_address)
       if cnt == 3:
           Device_2_ip_address = line
           print(Device_2_ip_address)
       line = fp.readline() 
       cnt += 1
   packet_to_sned = (hex(magic_packet_1) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address))
   print(packet_to_sned) 

def server():
    conn, addr = s.accept()
    print ('Connected by', addr)
    while 1:
        data = str(conn.recv(1024))
        print(data)
        if data == "b'PON'":
            print('Command Screen On Received')
            udp.sendto((hex(magic_packet_1) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address) + hex(Screen_mac_address)), (Screen_ip_address, UDP_PORT))
            
        if data == "b'POFF'":
            print('Command Screen Off Received')     
        if not data: break
    conn.close()
    print('Disconnected', addr)
    



while True:
    server()
Reply
#2
Why are you calling hex() on those values? That takes a number and returns a python string.

But udp.sendto sends bytes, not strings. If you want to send the string in a UTF-8 encoding, you can pass the string to encode() and that will generate a bytes object.

>>> type("foo")
<class 'str'>
>>> type("foo".encode())
<class 'bytes'>
If you instead want to send the raw bytes, you'll need to look at struct.pack()
Reply
#3
Hi

Sorry posted wrong code and error!!!!also just to let you know I am new to coding !!

Error:
Connected by ('127.0.0.1', 52572) b'PON' Command Screen On Received Traceback (most recent call last): File "c:/Python Code/new test.py", line 55, in <module> server() File "c:/Python Code/new test.py", line 43, in server udp.sendto(packet_to_sned.encode('utf-8'), (Screen_ip_address, UDP_PORT)) AttributeError: 'int' object has no attribute 'encode
import socket

magic_packet_1 = 0xFFFFFFFFFFFF
packet_to_sned = 0x00
Screen_mac_address = 0x00
Screen_ip_address = ''
Device_2_ip_address = ''
HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 50007
UDP_PORT = 9
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((HOST, PORT))
s.listen(1)

filepath = 'config.cfg'
with open(filepath) as fp:
   line = fp.readline()
   cnt = 1
   while line:
       if cnt == 1:
           Screen_mac_address = int(line,16)
           print(hex(Screen_mac_address))
       if cnt == 2:
           ip_address = line 
           print(ip_address)
       if cnt == 3:
           Device_2_ip_address = line
           print(Device_2_ip_address)
       line = fp.readline() 
       cnt += 1
   packet_to_sned = (magic_packet_1 + Screen_mac_address )
   print(packet_to_sned) 

def server():
    conn, addr = s.accept()
    print ('Connected by', addr)
    while 1:
        data = str(conn.recv(1024))
        print(data)
        if data == "b'PON'":
            print('Command Screen On Received')
            udp.sendto(packet_to_sned.encode('utf-8'), (Screen_ip_address, UDP_PORT))
            
        if data == "b'POFF'":
            print('Command Screen Off Received')     
        if not data: break
    conn.close()
    print('Disconnected', addr)
    



while True:
    server()
Reply
#4
magic_packet_1 is a number (an int).
Screen_mac_address is also a number (an int).

So line 32 (packet_to_sned = (magic_packet_1 + Screen_mac_address )) is also a number (since you're just adding two numbers).

But udp.sendto() doesn't take numbers, it takes a bytestring. So you have to decide do you want to pack that number into a set of bytes? Do you want a string representation of that number? Exactly what do you want sent?
Reply
#5
Hi

Thanks for the reply, what I am trying to achieve is a script that read a MAC address and IP address a file (config.cfg) i am using this data to send a WOL magic packet (0xFFFFFF followed by MAC address 16 times) to PORT 9 via UDP to the IP address read from the file
Reply
#6
You'll want to struct.pack those values.

There's an existing package on Pypi called wakeonlan. You can take a look through that code to see one method to do it. They use methods both of encoding text and packing numeric data.
Reply
#7
Thank you so much for your help !!!
Reply


Forum Jump:

User Panel Messages

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