S
S
Samosh20102021-09-19 16:16:38
Python
Samosh2010, 2021-09-19 16:16:38

Error decoding string to base64, what to do?

I'm trying to decode base64 bytes into a string, but that throws an error:

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

I'm trying to transfer a file through a socket, and that's what it takes to transfer pictures.
Server script:
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()

Client script:
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()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-09-19
@Samosh2010

Invalid base64-encoded string: number of data characters (1097) cannot be 1 more than a multiple of 4

At you the left data in a decoded line. Some newline (\n) or something like that didn't get stuck at the beginning/end?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question