1 """
 2 # 第一个知识点    \r
 3 """
 4 import time
 5 print('\r80%',end='')
 6 time.sleep(2)
 7 print('\r90%',end='')
 8 """
 9 # 第二个知识点    %%
10 """
11 tpl = "进度条目前是%s%%" %(90,)
12 print(tpl)
13 """
14 import time
15 
16 def func(size,total_size):
17     val = int(size/total_size * 100)
18     time.sleep(0.1)
19     print('\r%s%%|%s' %(val,"#"*val,), end='')
20 
21 for i in range(101):
22     func(i,100)
进度条原理示例
  1 ftp-server端
  2 
  3 import os
  4 import json
  5 import socketserver
  6 import shutil
  7 
  8 CODE = {
  9     '1001':'上传文件,从头开始上传'
 10 }
 11 
 12 def recv_file(code, mode, recv_size, upload_file_size):
 13     
 14     response = {'code': code, 'size': recv_size}
 15     conn.sendall(json.dumps(response).encode('utf-8'))
 16 
 17     f = open(file_md5_path, mode)
 18     while recv_size < upload_file_size:
 19         data = conn.recv(1024)
 20         f.write(data)
 21         f.flush()
 22         recv_size += len(data)
 23     f.close()
 24 
 25     # 3.1.3 改名字
 26     shutil.move(file_md5_path, file_name_path)
 27 
 28 
 29 def upload(cmd_dict,conn,username):
 30     """
 31     服务端完成上传文件(含断点续传)
 32     :param cmd_dict:
 33     :param conn:
 34     :return:
 35     """
 36     # 2. 获取文件信息
 37     file_md5 = cmd_dict['md5']
 38     file_name = cmd_dict['file_name']
 39 
 40     file_md5_path = os.path.join('home', username, file_md5)
 41     file_name_path = os.path.join('home', username, file_name)
 42     upload_file_size = cmd_dict['size']
 43 
 44     # 3. 判断文件是否存在
 45     exist = os.path.exists(file_md5_path)
 46     if not exist:  # 不续传
 47         # 3.1.1 可以开始上传了,我已经准备好。
 48         recv_file('1001', 'wb', 0, upload_file_size)
 49 
 50     else:  # 续传
 51         # 3.2 续传+大小
 52         exist_size = os.stat(file_md5_path).st_size
 53     recv_file('1002', 'ab', exist_size, upload_file_size)
 54 
 55 class NbServer(socketserver.BaseRequestHandler):
 56     def handle(self):
 57         """
 58         self.request 是客户端的socket对象
 59         :return:
 60         """
 61         # 1. 接收命令
 62         upload_cmd_bytes = self.request.recv(8096)
 63         cmd_dict = json.loads(upload_cmd_bytes.decode('utf-8'))
 64 
 65         if cmd_dict['cmd'] == 'upload':
 66             upload(cmd_dict,self.request,'zhangzijian')
 67         elif cmd_dict['cmd'] == 'download':
 68             pass
 69 
 70 if __name__ == '__main__':
 71     server = socketserver.ThreadingTCPServer(('127.0.0.1',8001),NbServer)
 72     server.serve_forever()
 73 
 74 
 75 
 76 
 77 ftp-client端
 78 
 79 import os
 80 import socket
 81 import json
 82 import hashlib
 83 CODE = {
 84     '1001':'上传文件,从头开始上传'
 85 }
 86 
 87 
 88 def file_md5(file_path):
 89     """
 90     文件进行md5加密
 91     :param file_path:
 92     :return:
 93     """
 94     obj = open(file_path,'rb')
 95     m = hashlib.md5()
 96     for line in obj:
 97         m.update(line)
 98     obj.close()
 99     return m.hexdigest()
100 
101 def jdt(size,total_size):
102     """
103     显示进度条
104     :return:
105     """
106     val = int(size / total_size * 100)
107     print('\r%s%%|%s' % (val, "#" * val,), end='')
108 
109 def send_file(exist_size,file_total_size):
110     """
111     发送文件
112     :param exist_size:开始读取字节的位置
113     :param file_total_size: 文件总字节大小
114     :return:
115     """
116     f = open(file_path, 'rb')
117     f.seek(exist_size)
118     send_size = exist_size
119     while send_size < file_total_size:
120         data = f.read(1024)
121         sk.sendall(data)
122         send_size += len(data)
123         jdt(send_size,file_total_size)
124     f.close()
125     print('上传成功')
126 
127 def upload(file_path):
128     """
129     文件上传(含断点)
130     :param file_path:
131     :return:
132     """
133     file_md5_val = file_md5(file_path)
134     file_name = os.path.basename(file_path)
135     file_size = os.stat(file_path).st_size
136 
137     cmd_dict = {'cmd': 'upload', 'file_name': file_name, 'size': file_size, 'md5': file_md5_val}
138     upload_cmd_bytes = json.dumps(cmd_dict).encode('utf-8')
139     sk.sendall(upload_cmd_bytes)
140 
141     # 2. 等待服务端的响应
142     response = json.loads(sk.recv(8096).decode('utf-8'))
143     if response['code'] == 1001:
144         send_file(0, file_size)
145     else:
146         # 断点续传
147         exist_size = response['size']
148         send_file(exist_size,file_size)
149 
150 sk = socket.socket()
151 sk.connect(('127.0.0.1',8001))
152 
153 while True:
154     # upload|文件路径
155     user_input = input("请输入要执行的命令")
156     # 1. 自定义协议{'cmd':'upload','file_path':'.....'}
157     cmd,file_path = user_input.split('|',maxsplit=1)
158     if cmd == 'upload':
159         upload(file_path)
160     elif cmd == 'download':
161         pass
文件上传(含断点续传)

 

posted on 2019-10-21 19:23  始终不够啊  阅读(298)  评论(0编辑  收藏  举报