How to send an image correctly via Socket in Python

Asked

Viewed 327 times

1

I am trying to send a picture of the dir directory to another client of my network. The 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()
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.