飞鸽传书-下载文件
FeiQTcp.py(部分修改)
def download_file(file_info):
"""下载文件"""
client_tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
option = "%x:%x:%x" % (file_info['packet_id'], file_info['file_id'], 0)
request_info = FeiQSend.build_msg(FeiQCoreData.IPMSG_GETFILEDATA, option)
client_tcp_socket.connect((file_info['dest_ip'], FeiQCoreData.feiq_port))
client_tcp_socket.send(request_info.encode("gbk"))
f = open(file_info['file_name'] ,"wb")
recv_data_length = 0
while True:
recv_data = client_tcp_socket.recv(1024)
if recv_data:
f.write(recv_data)
else:
break
recv_data_length += len(recv_data)
if recv_data_length >= file_info['file_size']:
break
f.close()
print("下载(%s)ok" % file_info['file_name'])
def get_file_msg_from_queue(file_queue):
"""从Queue 获取需要下载或者发送的文件信息"""
while True:
data_info = file_queue.get()
if data_info['type'] == "download_file":
print("需要下载。。。。", data_info['data'])
download_file(data_info['data'])
elif data_info['type'] == "send_file":
print("发送文件......", data_info['data'])
FeiQCoreData.send_file_list.append(data_info['data'])