程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python file upload - breakpoint continuation

編輯:Python

server.py The contents of the document are as follows

import socket
import os
import json
import hashlib
sk = socket.socket()
sk.bind(('127.0.0.1', 8000))
sk.listen(5)
conn, addr = sk.accept()
while True:
# Accept the data sent by the client , See if it exists md5 File name
recive_msg = json.loads(conn.recv(1024).decode('utf8'))
# md5 file name
md5_file_name = recive_msg['file_md5_value']
# file size
file_total_size = recive_msg['file_total_size']
# Original file size
origin_file_name = recive_msg['origin_file_name']
# Judge whether the file exists , Continuation if there is . Otherwise, from 0 Start passing
is_exists = os.path.exists('./upload/' + md5_file_name)
if not is_exists:
# from 0 Start uploading
conn.sendall(json.dumps({'code': 1001, 'msg': ' from 0 Start uploading '}).encode('utf8'))
# Accept the data from the client
recive_size = 0
f = open('./upload/' + md5_file_name, 'wb')
while recive_size < file_total_size:
data = conn.recv(1024)
f.write(data)
# Take the initiative to brush the data in memory into the file
f.flush()
recive_size += len(data)
f.close()
# After uploading , The file name of the modified file is the original file name
if os.path.getsize('./upload/' + md5_file_name) == file_total_size:
os.rename('./upload/' + md5_file_name, './upload/' + origin_file_name)
else:
# Breakpoint continuation , ( First, send the size of the received file to the client )
# Received size
has_recive_size = os.path.getsize('./upload/' + md5_file_name)
conn.sendall(json.dumps({'code': 1002, 'msg': ' Breakpoint continuation ', 'has_recive_size': has_recive_size}).encode('utf8'))
recive_size = has_recive_size
f = open('./upload/' + md5_file_name, 'ab')
while recive_size < file_total_size:
data = conn.recv(1024)
f.write(data)
# Take the initiative to brush the data in memory into the file
f.flush()
recive_size += len(data)
f.close()
# After uploading , The file name of the modified file is the original file name
if os.path.getsize('./upload/' + md5_file_name) == file_total_size:
os.rename('./upload/' + md5_file_name, './upload/' + origin_file_name)
break
conn.close()
sk.close()

client.py The contents of the document are as follows :

import socket
import os
import hashlib
import json
sk = socket.socket()
sk.connect(('127.0.0.1', 8000))
md5 = hashlib.md5()
# Check the file before uploading md5 value , If the server exists, continue the transmission , Otherwise, from 0 Start uploading
# The name of the temporary file is md5 value , After uploading successfully, change the file name to the real file name
while True:
file_name = input(' Please enter the name of the file to be uploaded :').strip()
# retrievable md5 value
with open(file_name, 'rb') as f:
for line in f:
md5.update(line)
md5_value = md5.hexdigest()
# Get the total size of the file
total_size = os.path.getsize(file_name)
# Combine the data sent
dic = {'file_md5_value': md5_value, 'file_total_size': total_size, 'origin_file_name': file_name}
dic_str_bytes = json.dumps(dic).encode('utf8')
# send data
sk.sendall(dic_str_bytes)
# Judge code value , Whether from 0 Start uploading
has_code_msg = json.loads(sk.recv(1024).decode('utf8'))
code = has_code_msg['code']
if code == 1001:
# from 0 Start uploading
try:
f = open(file_name, 'rb')
upload_size = 0
while upload_size < total_size:
data = f.read(1024)
sk.sendall(data)
upload_size += len(data)
# break # Test breakpoint
finally:
f.close()
else:
# Breakpoint continuation
# Get the uploaded size first
has_send_size = has_code_msg['has_recive_size']
try:
f = open(file_name, 'rb')
upload_size = has_send_size
f.seek(has_send_size)
while upload_size < total_size:
data = f.read(1024)
sk.sendall(data)
upload_size += len(data)
finally:
f.close()

The directory structure is as follows :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved