Я пытаюсь декодировать байты base64 в строку, но тот выдает ошибку:
C:\Users\maksa\Desktop\My Files\file_transfer\filse>python client.py Connected! 0%| | 0/100 [00:00<?, ?it/s] Traceback (most recent call last): File "C:\Users\maksa\Desktop\My Files\file_transfer\filse\client.py", line 24, in <module> Write() File "C:\Users\maksa\Desktop\My Files\file_transfer\filse\client.py", line 22, in Write f.write(base64.b64decode(content)) File "C:\Program Files\Python39\lib\base64.py", line 87, in b64decode return binascii.a2b_base64(s) binascii.Error: Invalid base64-encoded string: number of data characters (1097) cannot be 1 more than a multiple of 4
Я пытаюсь передавать файл через socket, и для передачи картинок он то и требуеться.
Скрипт сервера:
import socket, time, json, base64
from tqdm import tqdm
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("192.168.0.2", 8020))
server.listen(5)
print("Waiting Connection...")
user, addr = server.accept()
print("Connected!" + str(addr))
def Send(data):
json_data = json.dumps(str(data))
user.send(json_data.encode("utf-8"))
address = input("Select a file for send: ")
Send(address)
def Read():
for i in tqdm(range(100)):
with open(address, "rb") as f:
data = base64.b64encode(f.read())
Send(data)
Read()
Скрипт клиента:
import socket, time, json, base64
from tqdm import tqdm
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("192.168.0.2", 8020))
print("Connected!")
def Receive():
json_data = "".encode()
while True:
try:
json_data = json_data + client.recv(1024)
return json.loads(json_data.decode("utf-8"))
except ValueError:
continue
filename = Receive()
def Write():
content = Receive()
for i in tqdm(range(100)):
with open(filename, "wb") as f:
f.write(base64.b64decode(content))
Write()