I am trying to implement a way to send and receive files using the socket library, but when I run the code, I keep getting the error "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte".
Sender code:
import os, socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("localhost", 9999))
with open("image.jpg", "rb") as file:
file_size = os.path.getsize("send_file.txt")
client.send("image.jpg".encode())
client.send(str(file_size).encode())
data = file.read()
client.sendall(data)
client.send(b"<DATA_END>")
client.close()
Receiver code:
import socket, tqdm
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("localhost", 9999))
server.listen()
client, addr = server.accept()
file_name = client.recv(1024).decode()
print(file_name)
file_size = client.recv(1024).decode()
print(file_size)
with open(file_name, "wb") as file:
file_bytes = b""
done = False
progress = tqdm.tqdm(unit="B", unit_scale=True, unit_divisor=1000, total=int(file_size))
while not done:
data = client.recv(1024)
if file_bytes[-10:] == b"<DATA_END>":
done = True
else:
file_bytes += data
progress.update(1024)
file.write(file_bytes)
client.close()
server.close()
"image.jpg".encode(), for instance, that could be utf-8 one machine, utf-16 on another and latin1 on yet another. TheFFsuggests that you are reading a utf-16 BOM, but just a guess there. Put"utf-8"as the encoding for all encode and decode operations then retest..encode()isutf-8since Python 3.2, so not as variable as, say,open.