'python manually finish writing file

I have a program which receive a file.BIN from socket TCP/IP but I don't know how to exit the writing loop

Here is a piece of my code:

    with open(f"recv_{FILENAME}", "wb") as f:
        while True:
            data = client_tcp.recv(1024)
            if not data:
                break
            else:
                f.write(data)
                print("Data receiving")
                if 
                #I want to go out the loop here if the file is completely received                
        print("File Received")

Hope to get help from everybody. Many thanks



Sources

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

Source: Stack Overflow

Solution Source