'python: not write last bytes tcp?

Hello I wrote a simple programs that transfer files but I have a problem: the programs do not send me a complete file and stop in while condition.

Server source:

#simple transfer files server
import socket, sys

SRV_ADDR = ""
SRV_PORT = 9900

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((SRV_ADDR, SRV_PORT))
s.listen(1)
connection, address = s.accept()

while 1:
    pippo = connection.recv(1024)   

    if(pippo.decode('utf-8') == '3'):
       riceivename=connection.recv(1024)
       lettore=riceivename.decode('utf-8')
       file = open(lettore, 'rb') 

       while 1:
           data = file.read(1024)
           if not data: break
           connection.sendall(data)
       file.close()
       print("ok end cicle")   
           
           
    elif(data.decode('utf-8') == '0'):
          connection.close()
          connection, address = s.accept()

Client:

import sys
import socket
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect(('127.0.0.1', 9900))

def print_menu():
    print("""\n\n0) Close the connection
3) Receive file""")

print("Connection established")
print_menu()

while 1:
    message = input("\n-Select an option: ")

    if (message=="3"):
        sock.sendall(message.encode())
        localname=input("name with which you want to save the file:")
        file = open(localname, 'wb') 
        path=input("insert remote path of file to be downloaded:")
        sock.sendall(path.encode())

        while 1:
           data=sock.recv(1024)
           if not data:break
           file.write(data)
        file.close()

    elif(message=="0"):
         sock.sendall(message.encode())
         sock.close()
         break

    print_menu()

I think that the client does not write the last byte. I do not understand what the problem is.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source