zoukankan      html  css  js  c++  java
  • FTP服务器之下载

    1.SERVER端

    __author__ = 'alex'
    #coding:utf-8
    import socket
    import os
    import sys
    import json
    
    
    def process_bar(num,total):
        percent = float(num)/float(total)
        per_int = int(percent*100)
        # print (percent,per_int)
        # print (">"*per_int + "%d%%" %(per_int+1))
        temp = "
    %d%%" %(per_int)
        sys.stdout.write(temp)
        sys.stdout.flush()
    
    ip_port = ('127.0.0.1',8011)
    sk = socket.socket()
    sk.bind(ip_port)
    sk.listen(5)
    # print (os.path.dirname(__file__))
    print ("服务端启动...")
    file_info = {}
    
    while True:
        conn,addr = sk.accept()
        file_path = conn.recv(1024)
        file_path_str = str(file_path,'utf8')
        file_name = file_path_str.split('\')[-1]
        print (file_path_str)
        if os.path.exists(file_path_str):
            conn.sendall(bytes('2003','utf8'))      #告知客户端,服务器文件存在,可以下载
            conn.recv(1024)
            file_size = os.stat(file_path).st_size
            file_info['file_name'] = file_name
            file_info['file_size'] = file_size
            file_info_json = json.dumps(file_info)
            conn.sendall(bytes(file_info_json,"utf8"))
    
            client_info_recv = conn.recv(1024)
            client_info_recv_str = str(client_info_recv,'utf8')
            print (client_info_recv_str)
            client_repose_code = client_info_recv_str.split('|')[0]
            client_file_size = client_info_recv_str.split('|')[1]
            client_repose_code_int = int(client_repose_code)
            client_file_size_int = int(client_file_size)
            print (client_repose_code_int,client_file_size_int)
    
            if client_repose_code_int == 2004:      #客户端已经有了这个文件
                print ("客户端存在这个文件,现在开始断点续传...")
                file_obj = open(file_path_str,'rb')
                file_obj.seek(client_file_size_int)
                while client_file_size_int < file_size:
                        file_read = file_obj.read(1024)
                        conn.sendall(file_read)
                        client_file_size_int += len(file_read)
                        process_bar(client_file_size_int,file_size)
                file_obj.close()
    
            else :
                print ("客户端不存在这个文件,现在开始下载...")
                file_obj = open(file_path_str,'rb')
                while client_file_size_int < file_size:
                    file_read = file_obj.read(1024)
                    conn.sendall(file_read)
                    client_file_size_int += len(file_read)
                    process_bar(client_file_size_int,file_size)
                file_obj.close()
    
    conn.close()

    2.CLIENT端

    __author__ = 'alex'
    #coding:utf-8
    import socket
    import os
    import sys
    import json
    
    def process_bar(num,total):
        percent = float(num)/float(total)
        per_int = int(percent*100)
        # print (percent,per_int)
        # print (">"*per_int + "%d%%" %(per_int+1))
        temp = "
    %d%%" %(per_int)
        sys.stdout.write(temp)
        sys.stdout.flush()
    
    FILE_PATH = "D:\ftpRio.explorer.rmvb"         #你需要到服务器上去取得文件的目录
    FILE_NAME = FILE_PATH.split('\')[-1]
    print (FILE_NAME)
    
    ip_port = ('127.0.0.1',8011)
    sk = socket.socket()
    sk.connect(ip_port)
    file_info = {}
    print ("客户端启动...")
    
    while True:
        sk.sendall(bytes(FILE_PATH,"utf8"))
        file_exist_recv = sk.recv(1024)
        file_exist_recv_str = str(file_exist_recv,'utf8')
        if file_exist_recv_str == '2003':
            print ("服务器端文件存在,可以下载...")
            sk.sendall(bytes("ok",'utf8'))
            file_info_bytes = sk.recv(1024)
            file_info_str = str(file_info_bytes,"utf8")
            file_info = json.loads(file_info_str)
            server_file_name = file_info.get('file_name')
            server_file_size = file_info.get('file_size')
            print (file_info)
    
            if os.path.exists(server_file_name):
                client_file_size = os.stat(server_file_name).st_size
                print ("客户端已经有这个文件!")
                inp = input("文件是否需要续传?按y确认...")
                if inp == 'y':
                    sk.sendall(bytes(str(2004)+'|'+str(client_file_size),'utf8'))    #客户端已经有了这个文件
    
                client_file = open(server_file_name,'ab')
                while client_file_size < server_file_size:
                    try:
                        data=sk.recv(1024)
                        if not data:
                            raise Exception
                    except Exception:
                        break
    
                    client_file.write(data)
                    client_file_size += len(data)
                    process_bar(client_file_size,server_file_size)
                client_file.close()
    
            else:                   #客户端没有这个文件
                client_file_size = 0
                # sk.sendall(bytes(str(2005|client_file_size),'utf8'))
                sk.sendall(bytes(str(2005)+'|'+str(client_file_size),'utf8'))
                client_file = open(server_file_name,'wb')
                while client_file_size < server_file_size:
                    try:
                        data=sk.recv(1024)
                        if not data:
                            raise Exception
                    except Exception:
                        break
    
                    client_file.write(data)
                    client_file_size += len(data)
                    process_bar(client_file_size,server_file_size)
                client_file.close()
    
        else:
            print ("服务器端文件不存在,请查看文件输入是否正确!")
    sk.close()
  • 相关阅读:
    hdu 1290 献给杭电五十周年校庆的礼物 (DP)
    hdu 3123 GCC (数学)
    hdu 1207 汉诺塔II (DP)
    hdu 1267 下沙的沙子有几粒? (DP)
    hdu 1249 三角形 (DP)
    hdu 2132 An easy problem (递推)
    hdu 2139 Calculate the formula (递推)
    hdu 1284 钱币兑换问题 (DP)
    hdu 4151 The Special Number (DP)
    hdu 1143 Tri Tiling (DP)
  • 原文地址:https://www.cnblogs.com/python-study/p/5815610.html
Copyright © 2011-2022 走看看