首页 > 代码库 > sock_client_ftp.py

sock_client_ftp.py

import socket,hashlib
client = socket.socket()
client.connect(("localhost", 6971))   ##开始连接 (ServerIP,ServerPort)
while True:
    cmd = input("Please input the message>>").strip()
    print(cmd)
    if len(cmd) == 0:continue
    if cmd.startswith("get"):
        client.send(cmd.encode("utf-8"))
        server_response = client.recv(1024)
        print("server resonse:",server_response)
        client.send(b"ready yo recv file")
        file_total_size = int(server_response.decode())
        received_size = 0
        filename = cmd.split()[1]
        f = open(filename + ".new","wb")
        m = hashlib.md5()
        while received_size < file_total_size:
            if file_total_size - received_size > 1024:
                size = 1024
            else:
                size = file_total_size - received_size
                print("lase  recv:",size)
            data = client.recv(size)
        # data = http://www.mamicode.com/client.recv(1024)
            received_size +=len(data)
            m.update(data)
            f.write(data)
        else:
            new_file_md5 = m.hexdigest()
            f.close()
        server_file_md5 = client.recv(1024)
        print("server file md5:",server_file_md5)
        print("clinet file md5:",new_file_md5)

client.close()

 

sock_client_ftp.py