dms-client/util/upload_client.py
2022-02-28 13:51:30 +08:00

120 lines
4.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Author : XinYi Song
Time : 2021/12/1 15:19
Desc:
"""
import socket
import sys
import re
import os
from hashlib import md5
import time
FILE_DIR = os.path.dirname(__file__)
# BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# home = os.path.join(BASE_DIR, "/unstructured_data/remote_sensing_data")
home = "/unstructured_data/remote_sensing_data"
# 定义一个函数,计算进度条
def bar(num=1, sum=100):
rate = float(num) / float(sum)
rate_num = int(rate * 100)
temp = '\r%d %%' % (rate_num)
sys.stdout.write(temp)
def file_store_path(time_stamp):
"""
:param time_stamp: 时间戳类型时间
:return:
"""
now = int(round(time_stamp * 1000))
t = time.localtime(now / 1000)
return os.path.join('E:/data/upload', str(t[0]), str(t[1]), str(t[2]))
def file_store_path_year(data_str_time, upload_path, conllection_code):
"""
目录到年
:param upload_path:
:param data_str_time: 字符串类型
:return:
"""
t = time.strptime(data_str_time, '%Y-%m-%d %H:%M:%S')
return os.path.join(upload_path, conllection_code, str(t[0]))
def md5_file(name):
m = md5()
a_file = open(name, 'rb') # 需要使用二进制格式读取文件内容
m.update(a_file.read())
a_file.close()
return m.hexdigest()
def file_store_path_month(data_str_time, upload_path, conllection_code):
"""
目录到月
:param upload_path:
:param data_str_time:
:return:
"""
t = time.strptime(data_str_time, '%Y-%m-%d %H:%M:%S')
return os.path.join(upload_path, conllection_code, str(t[0]), str(t[1]))
def file_store_path_day(data_str_time, upload_path, conllection_code):
"""
目录到日
:param upload_path:
:param data_str_time: 字符串类型的时间
:return:
"""
t = time.strptime(data_str_time, '%Y-%m-%d %H:%M:%S')
return os.path.join(upload_path, conllection_code, str(t[0]), str(t[1]), str(t[2]))
def upload_file_client(file_path, depth, dateTime, collectionCode):
ck = socket.socket()
ck.connect(('192.168.2.9', 9002))
print(str(ck.recv(1024), encoding='utf-8'))
# inp = input('请输入内容格式post|文件路径 目标路径): \n >>> ').strip() # 输入内容格式:命令|文件路径 目标路径
# func, file_path = inp.split("|", 1) # 将输入的内容拆分为两部分,方法名和路径
# local_path, target_path = re.split("\s*", file_path, 1) #再将路径部分,通过正则表达式。以空格拆分为:文件路径和上传的目标路径
file_byte_size = os.stat(file_path).st_size # 获取文件的大小
file_name = os.path.basename(file_path) # 设置文件名
md5 = md5_file(file_path)
if depth == 'year':
file_paths = file_store_path_year(dateTime, home, collectionCode)
if depth == 'month':
file_paths = file_store_path_month(dateTime, home, collectionCode)
if depth == 'day':
file_paths = file_store_path_day(dateTime, home, collectionCode)
post_info = "post|%s|%s|%s|%s|%s" % (file_name, file_byte_size, collectionCode, dateTime, depth) # 将发送的内容进行拼接
ck.sendall(bytes(post_info, encoding='utf-8')) # 向服务器端发送内容
result_exist = str(ck.recv(1024), encoding='utf-8')
has_sent = 0
if result_exist == "2003":
ck.sendall(bytes("2004", encoding='utf-8'))
result_continue_pos = str(ck.recv(1024), encoding='utf-8') # 已经传输了多少的文件内容
print(result_continue_pos)
has_sent = int(result_continue_pos)
file_obj = open(file_path, 'rb') # 对文件进行读操作
file_obj.seek(has_sent) # 调整指针
while has_sent < file_byte_size:
data = file_obj.read(1024)
ck.sendall(data)
has_sent += len(data)
bar(has_sent, file_byte_size) # 进度条
file_obj.close()
print("文件上传成功!")
uc = {'file_size': file_byte_size, 'fileName': file_name, 'md5': md5, 'file_path': file_paths + '/' + file_name,
'type': 'ok'}
return uc