How to make the file finish receiving? Python Sockets

0

Good morning everyone, I am new to the world of Python ; I am developing an application with Sockets . It turns out that the code is developed for the version of Python 3 ; On the server side I have the problem that when I start the loop with the While True, it only performs 17 laps and ends the operation for each round the server saves 1024 bits, in 17 it would be 17408 bits. When you send files larger than that, it leaves them incomplete. Causing the client not to finish sending the file.

This is the code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#      server.py

from socket import socket, error
def main():
    s = socket()

    s.bind(("localhost", 6000))
    s.listen(0)

    conn, addr = s.accept()
    nombre = str(raw_input("nombre de archivo a guardar: "))

    archivoEnviar = open(nombre, "wb")
    #comprobación =open
    while True:
        try:
            # Recibir datos del cliente.
            input_data = conn.recv(1024)
        except error:
            print("Error de lectura.")
            break
        else:
            if input_data:
                # Compatibilidad con Python 3.
                if isinstance(input_data, bytes):
                    end = input_data[0] == 1
                else:
                    end = input_data == chr(1)
                if not end:
                    # Almacenar datos.
                    archivoEnviar.write(input_data)
                else:
                    break

    print("El archivo se ha recibido correctamente.")
    archivoEnviar.close()

if __name__ == "__main__":
    main() 

#!/usr/bin/env python
# -*- coding: utf-8 -*-


from socket import socket
import subprocess
import datetime
def main():
    s = socket()
    s.connect(("localhost", 6000))


    while True:
        archivo = str(input("Nombre del archivo a enviar, seguido del formato (Ej. cosa.mp3)"))
        # Con esto obtenemos la hora/fecha
        hora_actual = str(datetime.datetime.now())
        archivoEnviar = open(archivo, "rb")
        #Aquí voy a crear el archivo .sha256
        sha256 = subprocess.call(('sha256sum '+archivo+'>'+archivo+'.sha256'), shell=True)
        # Aqui voy a abrir el archivo que debo crear con la extensión .sha256
        registro= open("registro.txt", "a")
        comprobacion = open(sha256, "rb")
        registro.write("HORA: "+hora_actual+"   ARCHIVO ENVIADO: "+archivo+"\n")
        registro.write("************************\n")
        content = archivoEnviar.read(1024)
        EnvioComprobacion = comprobacion.read(1024)
        # Aquí voy a enviar el archivo también

        while content:
            # Enviar contenido.
            s.send(content)
            s.send(EnvioComprobacion)
            content = archivoEnviar.read(1024)
            EnvioComprobacion= comprobacion.read(1024)
        break

    # Se utiliza el caracter de código 1 para indicar
    # al cliente que ya se ha enviado todo el contenido.
    try:
        s.send(chr(1))
    except TypeError:
        # Compatibilidad con Python 3.
        s.send(bytes(chr(1), "utf-8"))

    # Cerrar conexión y archivo.
    s.close()
    archivoEnviar.close()
    registro.close()
    print("El archivo ha sido enviado correctamente.")
if __name__ == "__main__":
    main()
    
asked by Luiz Carranza 12.11.2018 в 19:30
source

0 answers