Python implementation file downloader

Posted by ccl on Sat, 19 Feb 2022 20:53:41 +0100

Python - implement file downloader

Supplementary knowledge points

  1. tcp servers generally need to be bound, otherwise the client cannot find the server
  2. tcp clients are generally not bound, because they send out the linked server, so as long as the ip, port and other information of the server is determined, the local client can be randomly connected
  3. In tcp server, the active socket created by socket can be changed into passive socket through listen, which is necessary for tcp server
  4. When the client needs to link to the server, it needs to use connect to link. udp does not need to link, but sends it directly. However, tcp must link first and can communicate only after the link is successful
  5. When a tcp client connects to the server, the server will have a new socket, which is used to mark the client and serve the client alone
  6. The socket after listen is a passive socket, which is used to receive the link request of a new client, while the new socket returned by accept marks the client
  7. Closing the socket after listen ing means that the passive socket is closed, which will make the new client unable to link to the server, but the previously successfully linked client can communicate normally
  8. Closing the socket returned by accept means that the client has finished serving
  9. When the client's socket calls close, the server will recv unblock and its return length is 0. Therefore, the server can distinguish whether the client has been offline by the length of the returned data

File downloader client

#!/usr/bin/env python
# coding=utf-8
import socket


def main():
    #1. Create socket
    tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    #2. Get the server ipport
    dest_ip = input("please input server ip:")
    dest_port = int(input("please input server port"))

    #3. Linked server
    tcp_socket.connect((dest_ip, dest_port))

    #4. Get the name of the downloaded file
    download_file_name = input("please input filename:")

    #5. Send the file name to the server
    tcp_socket.send(download_file_name.encode("utf-8"))

    #6. Receive the data in the file
    recv_data = tcp_socket.recv(1024) # 1024---->1k   1024*1024---->1k*1024=1M
    
    if recv_data:
        #7. Save the received data to a file
        with open("[new]" + download_file_name, "wb") as f:
            f.write(recv_data)

    #8. Close the socket
    tcp_socket.close()

if __name__ == "__main__":
    main()

File downloader server

#!/usr/bin/env python
# coding=utf-8
import socket

def send_file_2_client(new_client_socket, client_addr):
    #1. Receive the file name downloaded by the client
    # Receive the file name sent by the client to download
    file_name = new_client_socket.recv(1024).decode("utf-8")
    print("Client(%s)The files to be downloaded are:%s" % (str(client_addr), file_name))
    
    file_content = None
    #2. Open the file and read the data
    try:
        f = open(file_name, "rb")
        file_content = f.read()
        f.close()
    except Exception as ret:
        print("There are no files to download(%s)" % file_name)

    #3. Send file data to the client
    if file_content:
        #new_client_socket.send("hahhhahahah".encode("utf-8"))
        new_client_socket.send(file_content)

def main():
    #1. Create socket
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    #2. Bind local information
    tcp_server_socket.bind(("", 7788))

    #3. Change the default socket from active to passive
    tcp_server_socket.listen(128)

    while True:
        #4. Wait for the link from the client
        new_client_socket, client_addr = tcp_server_socket.accept()
        
        #5 send file to client
        send_file_2_client(new_client_socket, client_addr)

        #6. Close the socket
        new_client_socket.close()
    tcp_server_socket.close()



if __name__ == "__main__":
    main()

Topics: Python Linux network socket