How to send an image correctly via Socket in Python

1

I'm trying to send an image from the 'dir' directory to another client in my network. Sockets are working properly, the image arrives at the destination and is saved, but when I try to open it I get: "This is not a valid bitmap file."

    #Código do rementente
    with open(dir, 'rb') as f:
        bits = f.read(2048)
        aux3 = len(bits)
        s.sendall(bits.encode('utf-8'))
        while aux3 < tam:
            if bits != '':
                #bits = base64.b64encode(f.read(2048))
                s.sendall(bits.encode('utf-8'))
                time.sleep(0.5)
                aux3 += len(bits)
            else:
                break
    f.close()

Here's the other part:

 #Código do Destinatário
           with open(nome, 'wb') as f:
                while x < tam:
                    try:
                        arq = s.recv(2048)
                        x += len(arq)
                        arq = arq.decode('utf-8')
                        print 'Receebndo a imagem.'
                        print 'Receebndo a imagem..'
                        print 'Receebndo a imagem...'
                        # arq1 = base64.b64decode(arq)
                        #arq = arq.decode('base64')
                        f.write(arq)
                    except Exception as e:
                        print e
                        pass
                print 'Arquivo recebido com sucesso'
                f.close()
    
asked by anonymous 10.12.2017 / 01:26

0 answers