python note 30 断点续传

1、进度条

import time

def func(size,total_size):
    val = int(size/total_size * 100)
    time.sleep(0.1)
    print('\r%s%%|%s' %(val,"#"*val,), end='')

for i in range(101):
    func(i,100)

2、计算文件大小

import os

size = os.stat(r'D:\sylar\s15\day31\1.进度条.py').st_size
print(size)

3、修改文件名称

import os
import shutil
os.rename('a.text','b.txt')#py2 + win:报错
shutil.move('c.txt','a.txt')

4、图标

v = '\ue056'
print(v)

5、client

import os
import socket
import json
import hashlib
CODE = {
    '1001':'上传文件,从头开始上传'
}


def file_md5(file_path):
    """
    文件进行md5加密
    :param file_path:
    :return:
    """
    obj = open(file_path,'rb')
    m = hashlib.md5()
    for line in obj:
        m.update(line)
    obj.close()
    return m.hexdigest()

def jdt(size,total_size):
    """
    显示进度条
    :return:
    """
    val = int(size / total_size * 100)
    print('\r%s%%|%s' % (val, "#" * val,), end='')

def send_file(exist_size,file_total_size):
    """
    发送文件
    :param exist_size:开始读取字节的位置
    :param file_total_size: 文件总字节大小
    :return:
    """
    f = open(file_path, 'rb')
    f.seek(exist_size)
    send_size = exist_size
    while send_size < file_total_size:
        data = f.read(1024)
        sk.sendall(data)
        send_size += len(data)
        jdt(send_size,file_total_size)
    f.close()
    print('上传成功')

def upload(file_path):
    """
    文件上传(含断点)
    :param file_path:
    :return:
    """
    file_md5_val = file_md5(file_path)
    file_name = os.path.basename(file_path)
    file_size = os.stat(file_path).st_size

    cmd_dict = {'cmd': 'upload', 'file_name': file_name, 'size': file_size, 'md5': file_md5_val}
    upload_cmd_bytes = json.dumps(cmd_dict).encode('utf-8')
    sk.sendall(upload_cmd_bytes)

    # 2. 等待服务端的响应
    response = json.loads(sk.recv(8096).decode('utf-8'))
    if response['code'] == 1001:
        send_file(0, file_size)
    else:
        # 短点续传
        exist_size = response['size']
        send_file(exist_size,file_size)

sk = socket.socket()
sk.connect(('127.0.0.1',8001))

while True:
    # upload|文件路|径
    user_input = input("请输入要执行的命令")
    # 1. 自定义协议{'cmd':'upload','file_path':'.....'}
    cmd,file_path = user_input.split('|',maxsplit=1)
    if cmd == 'upload':
        upload(file_path)
    elif cmd == 'download':
        pass

6、server

import os
import json
import socketserver
import shutil

CODE = {
    '1001':'上传文件,从头开始上传'
}

def upload(cmd_dict,conn,username):
    """
    服务端完成上传文件(含断点续传)
    :param cmd_dict:
    :param conn:
    :return:
    """
    # 2. 获取文件信息
    file_md5 = cmd_dict['md5']
    file_name = cmd_dict['file_name']

    file_md5_path = os.path.join('home', username, file_md5)
    file_name_path = os.path.join('home', username, file_name)
    upload_file_size = cmd_dict['size']

    # 3. 判断文件是否存在
    exist = os.path.exists(file_md5_path)
    if not exist:  # 不续传
        # 3.1.1 可以开始上传了,我已经准备好。
        response = {'code': 1001}
        conn.sendall(json.dumps(response).encode('utf-8'))

        # 3.1.2 接收上传的文件内容
        f = open(file_md5_path, 'wb')
        recv_size = 0
        while recv_size < upload_file_size:
            data = conn.recv(1024)
            f.write(data)
            f.flush()
            recv_size += len(data)
        f.close()

        # 3.1.3 改名字
        shutil.move(file_md5_path, file_name_path)

    else:  # 续传
        # 3.2 续传+大小
        exist_size = os.stat(file_md5_path).st_size
        response = {'code': 1002, 'size': exist_size}
        conn.sendall(json.dumps(response).encode('utf-8'))

        f = open(file_md5_path, 'ab')
        recv_size = exist_size
        while recv_size < upload_file_size:
            data = conn.recv(1024)
            f.write(data)
            f.flush()
            recv_size += len(data)
        f.close()

        # 3.1.3 改名字
        shutil.move(file_md5_path, file_name_path)

class NbServer(socketserver.BaseRequestHandler):
    def handle(self):
        """
        self.request 是客户端的socket对象
        :return:
        """
        # 1. 接收命令
        upload_cmd_bytes = self.request.recv(8096)
        cmd_dict = json.loads(upload_cmd_bytes.decode('utf-8'))

        if cmd_dict['cmd'] == 'upload':
            upload(cmd_dict,self.request,'zhangzijian')
        elif cmd_dict['cmd'] == 'download':
            pass

if __name__ == '__main__':
    server = socketserver.ThreadingTCPServer(('127.0.0.1',8001),NbServer)
    server.serve_forever()

 

posted @ 2019-07-16 22:34  P-Z-W  阅读(170)  评论(0编辑  收藏  举报