81 lines
3.1 KiB
Python
81 lines
3.1 KiB
Python
"""
|
||
Author : XinYi Song
|
||
Time : 2021/12/1 15:19
|
||
Desc:
|
||
"""
|
||
import socket
|
||
import sys
|
||
import re
|
||
import os
|
||
from hashlib import md5
|
||
|
||
from util.file_store_path import file_store_path_year, file_store_path_month, file_store_path_day
|
||
|
||
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 md5_file(name):
|
||
m = md5()
|
||
a_file = open(name, 'rb') #需要使用二进制格式读取文件内容
|
||
m.update(a_file.read())
|
||
a_file.close()
|
||
return m.hexdigest()
|
||
|
||
|
||
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":
|
||
# inp = input("文件已存在,是否续传?Y/N:").strip()
|
||
# if inp.upper() == 'Y':
|
||
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)
|
||
|
||
# else:
|
||
# ck.sendall(bytes("2005", encoding='utf-8')) #发送2005代表不续传
|
||
|
||
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
|