current position:Home>Python: realize the function of receiving file (with complete source code)

Python: realize the function of receiving file (with complete source code)

2022-07-25 10:38:50Full stack technology blog

Python: Realization receive file Receive file function

if __name__ == "__main__":
    import socket  # Import socket module

    sock = socket.socket()  # Create a socket object
    host = socket.gethostname()  # Get local machine name
    port = 12312

    sock.connect((host, port))
    sock.send(b"Hello server!")

    with open("Received_file", "wb") as out_file:
        print("File opened")
        print("Receiving data...")
        while True:
            data = sock.recv(1024)
            print(f"{
      data = }")
            if not data:
                break
            out_file.write(data)  # Write data to a file

    print("Successfully got the file")
    sock.close()
    print("Connection closed")

copyright notice
author[Full stack technology blog],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/206/202207250933525211.html

Random recommended