优化代码

This commit is contained in:
XinYi Song 2021-12-03 17:59:26 +08:00
parent 05df024ddc
commit c27ff27f9e

View File

@ -17,6 +17,7 @@ FILE_DIR = os.path.dirname(__file__)
# home = os.path.join(BASE_DIR, "/unstructured_data/remote_sensing_data") # home = os.path.join(BASE_DIR, "/unstructured_data/remote_sensing_data")
home = "/unstructured_data/remote_sensing_data" home = "/unstructured_data/remote_sensing_data"
# 定义一个函数,计算进度条 # 定义一个函数,计算进度条
def bar(num=1, sum=100): def bar(num=1, sum=100):
rate = float(num) / float(sum) rate = float(num) / float(sum)
@ -24,12 +25,13 @@ def bar(num=1, sum=100):
temp = '\r%d %%' % (rate_num) temp = '\r%d %%' % (rate_num)
sys.stdout.write(temp) sys.stdout.write(temp)
def md5_file(name): def md5_file(name):
m = md5() m = md5()
a_file = open(name, 'rb') #需要使用二进制格式读取文件内容 a_file = open(name, 'rb') # 需要使用二进制格式读取文件内容
m.update(a_file.read()) m.update(a_file.read())
a_file.close() a_file.close()
return m.hexdigest() return m.hexdigest()
def upload_file_client(file_path, depth, dateTime, collectionCode): def upload_file_client(file_path, depth, dateTime, collectionCode):
@ -55,16 +57,10 @@ def upload_file_client(file_path, depth, dateTime, collectionCode):
result_exist = str(ck.recv(1024), encoding='utf-8') result_exist = str(ck.recv(1024), encoding='utf-8')
has_sent = 0 has_sent = 0
if result_exist == "2003": if result_exist == "2003":
# inp = input("文件已存在是否续传Y/N:").strip()
# if inp.upper() == 'Y':
ck.sendall(bytes("2004", encoding='utf-8')) ck.sendall(bytes("2004", encoding='utf-8'))
result_continue_pos = str(ck.recv(1024), encoding='utf-8') # 已经传输了多少的文件内容 result_continue_pos = str(ck.recv(1024), encoding='utf-8') # 已经传输了多少的文件内容
print(result_continue_pos) print(result_continue_pos)
has_sent = int(result_continue_pos) has_sent = int(result_continue_pos)
# else:
# ck.sendall(bytes("2005", encoding='utf-8')) #发送2005代表不续传
file_obj = open(file_path, 'rb') # 对文件进行读操作 file_obj = open(file_path, 'rb') # 对文件进行读操作
file_obj.seek(has_sent) # 调整指针 file_obj.seek(has_sent) # 调整指针
@ -76,5 +72,6 @@ def upload_file_client(file_path, depth, dateTime, collectionCode):
file_obj.close() file_obj.close()
print("文件上传成功!") print("文件上传成功!")
uc = {'file_size': file_byte_size, 'fileName': file_name, 'md5': md5, 'file_path': file_paths+'/'+file_name, 'type': 'ok'} uc = {'file_size': file_byte_size, 'fileName': file_name, 'md5': md5, 'file_path': file_paths + '/' + file_name,
'type': 'ok'}
return uc return uc