Esempio n. 1
0
def post(conn, inp):
    # post|F:\BaiduNetdiskDownload\day30-python 全栈开发-基础篇\30-02 python 全栈开发-基础篇-day30 FTP作业示例代码(一).avi
    # inp  ===> post|D:/1.txt|2.txt
    commad_list = inp.split("|")
    local_path = commad_list[1]
    target_path = ''

    if len(commad_list) == 3:
        target_path = commad_list[2]
    else:
        local_path = local_path.replace('\\', '/')
        target_path = local_path.split('/')[-1]

    # 获取本地文件大小
    file_byte_size = os.stat(local_path).st_size
    # 获取本地文件名
    file_name = os.path.basename(local_path)
    # 获取本地文件md5值
    file_md5 = commons.fetch_file_md5(local_path)

    post_info = "post|%s|%s|%s|%s" % (file_byte_size, file_name, file_md5,
                                      target_path)

    conn.sendall(post_info.encode(settings.CHARSET))

    result_recv = json.loads(
        conn.recv(settings.RECV_SIZE).decode(settings.CHARSET))

    has_sent = 0

    if result_recv['code'] == settings.CODE['FILE_EXIST']['code']:
        inp_continue = input('文件已经存在,是否续传?Y/N').strip()

        if inp_continue.upper() == "Y":
            conn.sendall(
                str(settings.CODE['CONTINUED']['code']).encode(
                    settings.CHARSET))
            has_sent = conn.recv(settings.RECV_SIZE).decode(settings.CHARSET)
            has_sent = int(has_sent)
        else:
            conn.sendall(settings.CODE['NO_CONTINUED']['code'].encode(
                settings.CHARSET))

    file_obj = open(local_path, 'rb')
    file_obj.seek(has_sent)

    while file_byte_size > has_sent:
        data = file_obj.read(settings.RECV_SIZE)

        conn.sendall(data)

        has_sent += len(data)

        commons.bar(has_sent, file_byte_size)

    file_obj.close()
    print('上传成功')
Esempio n. 2
0
def post(conn, inp):
    # inp  ===> post|D:/1.txt     2.txt
    method, file_paths = inp.split("|", 1)
    local_path, target_path = re.split('\s*', file_paths, 1)
    # 获取本地文件大小
    file_byte_size = os.stat(local_path).st_size
    # 获取本地文件名
    file_name = os.path.basename(local_path)
    # 获取本地文件md5值
    file_md5 = commons.fetch_file_md5(local_path)

    post_info = "post|%s|%s|%s|%s" % (file_byte_size, file_name, file_md5,
                                      target_path)

    conn.sendall(bytes(post_info, 'utf-8'))

    result_exist = str(conn.recv(1024), 'utf-8')

    if result_exist == '4001':
        login(conn)
        return

    has_sent = 0
    if result_exist == "2003":
        inp_continue = input('文件已经存在,是否续传?Y/N')
        if inp_continue.upper() == "Y":
            conn.sendall(bytes('2004', 'utf-8'))
            result_continue_pos = str(conn.recv(1024), 'utf-8')
            has_sent = int(result_continue_pos)
        else:
            conn.sendall(bytes('2005', 'utf-8'))

    file_obj = open(local_path, 'rb')
    file_obj.seek(has_sent)
    while file_byte_size > has_sent:
        data = file_obj.read(1024)
        conn.sendall(data)
        has_sent += len(data)
        commons.bar(has_sent, file_byte_size)
    file_obj.close()
    print('上传成功')
Esempio n. 3
0
def post(conn, inp):
    method, file_paths = inp.split('|', 1)
    local_path, target_path = re.split('\s', file_paths, 1)
    file_byte_size = os.stat(local_path).st_size
    file_name = os.path.basename(local_path)
    file_md5 = commons.fetch_file_md5(local_path)

    post_info = "post|%s|%s|%s|%s" % (file_byte_size, file_name, file_md5,
                                      target_path)

    conn.sendall(bytes(post_info, 'utf-8'))

    result_exist = str(conn.recv(1024), 'utf-8')

    if result_exist == '4001':
        login(conn)
        return

    has_send = 0
    if result_exist == '2003':
        imp_continue = input(
            'File has exist. Do you want to continue upload? Y/N')
        if imp_continue.upper() == 'Y':
            conn.sendall(bytes('2004', 'utf-8'))
            result_continue_pos = str(conn.recv(1024), 'utf-8')
            has_send = int(result_continue_pos)
        else:
            conn.sendall(bytes('2005', 'utf-8'))

    file_obj = open(local_path, 'rb')
    file_obj.seek(has_send)
    while file_byte_size > has_send:
        data = file_obj.read(1024)
        conn.sendall(data)
        has_send += len(data)
        commons.bar(has_send, file_byte_size)
    file_obj.close()
    print('Upload finished successfully')