Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python error: TypeError: sequence item 0: expected a bytes-like object, str found?

#!/usr/bin/env python3

import socket  
UDP_IP = "127.0.0.1"
UDP_PORT = 3289
sock = socket.socket(socket.AF_INET, # Internet
                        socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
    
while True:
    data, addr = sock.recvfrom(4068) # buffer size is 4068 bytes 
    print("received message: %s" % data)
    out = ["EPSON",
            "Q",        # PacketType (Q for query and C for command)
            "\x03",     # DeviceType(3) (fixed)
            "\x00",     # DeviceNumber(0) (fixed)
            "\x00\x10", # Function(0010h)
            "\x00\x00", # Result (fixed?)
            "\x00\x00", # parameter length Length
            ""]         # command parameter
    l = len("".join(out))
    
    sock.sendto("".join(out), (addr[0], addr[1])) <--- error

I get TypeError: sequence item 0: expected a bytes-like object, str found on the line indicated above. I have also tried

sock.sendto(b"".join(out), (addr[0], addr[1]))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

b"".join(out) would expect an iterable of bytes like objects, However, yours are strings. Try

"".join(out).encode()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading