def recv_file(self): print("进入小文件接收函数") self.tcp_client.send(package_data_2_security(data2byte(91100))) # 接受客户端介绍信息的长度 json_data_len_buffer = self.tcp_client.recv(4) json_data_len_buffer_unpackage = unpackage_data_2_security( json_data_len_buffer) json_data_len = byte2data(json_data_len_buffer_unpackage) # 接受介绍信息的内容 json_data_buffer = self.tcp_client.recv(json_data_len) json_data_buffer_unpackage = unpackage_data_2_security( json_data_buffer) # 将介绍信息的内容反序列化 json_data = json.loads(json_data_buffer_unpackage.decode('utf-8')) print("本次介绍信息的内容为:{}".format(json_data)) print("本次file长度为:{}".format(json_data["filesize"])) # # 文件接收 print("进入接收区") filename = json_data['filename'] filesize = json_data['filesize'] filepath = json_data['filepath'] aim_device = json_data['aim_device'] md5 = json_data['md5'] recv_len = 0 with open(self.file_local_save_name + filename, 'wb') as sf: now_data_number = 0 while recv_len < filesize: package_data_buffer = self.tcp_client.recv(1028) package_data_buffer_un = unpackage_data_2_security( package_data_buffer) # print("Data Length {}".format(len(package_data_buffer_un))) if len(package_data_buffer_un) == 1028: file_buffer = package_data_buffer_un[0:1024] data_number = byte2data(package_data_buffer_un[1024:1028]) # print("Now Data Number {}, Data Number {}, ".format(now_data_number, data_number)) if now_data_number == data_number: if filesize - recv_len > buffsize: recv_len += len(file_buffer) sf.write(file_buffer) else: end_size = filesize - recv_len recv_len += end_size sf.write(file_buffer[0:end_size]) self.tcp_client.send( package_data_2_security(data2byte(91100))) now_data_number += 1 else: continue else: continue new_file_md5 = getmd5(self.file_local_save_name + filename) if new_file_md5 == md5: print("new_file_md5 {}, md5 {}".format(new_file_md5, md5)) print("文件接收成功") tip_message = "91100" + "+0~^D" + "文件接收成功" self.my_signal.emit(tip_message)
def run(self): print(self.ip, self.port) self.tcp_client = socket(AF_INET, SOCK_STREAM) ip_port = ((self.ip, int(self.port))) self.tcp_client.connect_ex(ip_port) print("开始一次接收文件") out_flag = False count_retry = 0 while not out_flag: try: self.recv_file() confirm_data = byte2data( unpackage_data_2_security(self.tcp_client.recv(4))) count_retry = 0 if confirm_data == 911000: print("NetThreadSendFileLite文件拆分包发送完成") os.remove(self.file_path) out_flag = True elif confirm_data == 911002: count_retry += 1 print("NetThreadSendFileLite文件拆分包发送出现错误") if count_retry == 2: break else: print("未知错误") out_flag = True except: count_retry += 1 if count_retry == 2: break print(traceback.format_exc()) self.tcp_client.close()
def start_net_fun(self): print(self.host, self.port) self.tcp_client = socket(AF_INET, SOCK_STREAM) ip_port = ((self.host, int(self.port))) self.tcp_client.connect_ex(ip_port) try: print('向服务端发送本机信息') json_message = {'client_name': self.device_name} json_message = json.dumps(json_message) json_message_buffer = json_message.encode('utf-8') json_message_buffer = package_data_2_security(json_message_buffer) self.tcp_client.send(package_data_2_security(data2byte(1000))) json_message_len_buffer = data2byte(len(json_message_buffer)) json_message_len_buffer = package_data_2_security( json_message_len_buffer) self.tcp_client.send(json_message_len_buffer) # 这里是4个字节 self.tcp_client.send(json_message_buffer) # 发送报头的内容 success_code_buffer = self.tcp_client.recv(4) success_code = byte2data( unpackage_data_2_security(success_code_buffer)) if success_code == 1000: self.is_connection = True except: self.is_connection = False print("Error{}, is_connection={}".format("网络错误,重置网络标识,启动服务器链接监测", self.is_connection))
def recv_big_file(self): print("进入大文件接收函数") # self.tcp_client.send(package_data_2_security(data2byte(110041))) # 接受客户端介绍信息的长度 json_data_len_buffer = self.tcp_client.recv(4) json_data_len_buffer_unpackage = unpackage_data_2_security( json_data_len_buffer) json_data_len = byte2data(json_data_len_buffer_unpackage) # 接受介绍信息的内容 json_data_buffer = self.tcp_client.recv(json_data_len) json_data_buffer_unpackage = unpackage_data_2_security( json_data_buffer) # 将介绍信息的内容反序列化 json_data = json.loads(json_data_buffer_unpackage.decode('utf-8')) print("本次介绍信息的内容为:{}".format(json_data)) print("本次file长度为:{}".format(json_data["filesize"])) # # 文件接收 print("进入接收区") filename = json_data['filename'] filesize = json_data['filesize'] filepath = json_data['filepath'] aim_device = json_data['aim_device'] md5 = json_data['md5'] use_thread_number = 5 print( "filename {}, filesize {}, filepath {}, aim_device {}, md5 {}\n进入大文件接收线程" .format(filename, filesize, filepath, aim_device, md5)) for number in range(use_thread_number): file_recv_thread = FileRecvQThread(number, self.host, self.port) file_recv_thread.start()
def send_text_message(self): try: self.tcp_client.send(package_data_2_security(data2byte(1011))) print('向服务端发送文本消息') json_message = { 'client_name': self.text_message_aim_device, 'security_md5_text': self.text_message } json_message = json.dumps(json_message) json_message_buffer = json_message.encode('utf-8') json_message_buffer = package_data_2_security(json_message_buffer) json_message_len_buffer = data2byte(len(json_message_buffer)) json_message_len_buffer = package_data_2_security( json_message_len_buffer) self.tcp_client.send(json_message_len_buffer) # 这里是4个字节 self.tcp_client.send(json_message_buffer) # 发送报头的内容 success_code_buffer = self.tcp_client.recv(4) success_code = byte2data( unpackage_data_2_security(success_code_buffer)) if success_code == 1011: self.is_send_heartbeat = True self.send_code = -1 except Exception as e: print(e.args) print(traceback.format_exc()) print("Error{}, is_connection={}".format("网络错误,重置网络标识,启动服务器链接监测", self.is_connection))
def send_file(self): head_info_len, head_info, file_size = get_file_inform(self.file_path, self.aim_device) print(head_info) self.tcp_client.send(package_data_2_security(data2byte(11000))) confirm_data = byte2data(unpackage_data_2_security(self.tcp_client.recv(4))) if confirm_data == 911000: head_info_buffer = head_info.encode('utf-8') head_info_buffer = package_data_2_security(head_info_buffer) head_info_len_buffer = data2byte(len(head_info_buffer)) head_info_len_buffer = package_data_2_security(head_info_len_buffer) self.tcp_client.send(head_info_len_buffer) # 这里是4个字节 self.tcp_client.send(head_info_buffer) # 发送报头的内容 s = time.time() counter = 0 with open(self.file_path, 'rb') as sf: while True: data = sf.read(1024) data_len = len(data) if data_len == 0: break elif data_len == 1024: data = data + data2byte(counter) elif data_len < 1024: for i in range(data_len, 1024): data += b'0' data = data + data2byte(counter) timeout_counter = 0 while True: try: self.tcp_client.send(package_data_2_security(data)) confirm_data = byte2data(unpackage_data_2_security(self.tcp_client.recv(4))) if confirm_data == 911000: # print(data) # print(len(data)) # print(counter) counter += 1 timeout_counter = 0 break except socket.timeout as e: timeout_counter += 1 print(e.args) print(traceback.format_exc()) print("大文件数据发送超时,尝试重新发送") if timeout_counter == 5: return
def rece_text_message(self): confirm_code_buffer = self.tcp_client.recv(4) confirm_code = byte2data( unpackage_data_2_security(confirm_code_buffer)) if confirm_code == 1011: json_data_len_buffer = self.tcp_client.recv(4) json_data_len = byte2data( unpackage_data_2_security(json_data_len_buffer)) json_data_buffer = self.tcp_client.recv(json_data_len) json_data = unpackage_data_2_security(json_data_buffer) json_data = json_data.decode('utf-8') json_data = json.loads(json_data) text_message = "91011" + "+0~^D" + json_data['security_md5_text'] self.my_signal.emit(text_message) self.recv_code = -1 self.is_send_heartbeat = True
def heartbeat(self): print("进入心跳包发送阶段") success_code = 0 try: self.tcp_client.send(package_data_2_security(data2byte(1010))) success_code_buffer = self.tcp_client.recv(4) success_code = byte2data( unpackage_data_2_security(success_code_buffer)) if success_code == 1010: print("心跳包接收的是{}".format(success_code)) except ValueError: print("值错误{},进行服务重连或服务切换".format(ValueError)) self.is_connection = False except Exception as e: print(e.args) print("==============") print(traceback.format_exc()) print("{}".format("心跳保持出现问题,进行服务重连或切换")) self.is_connection = False if success_code == 1010: status = '在线9' elif success_code == 91011: status = '有文本信息' self.recv_code = 91011 self.is_send_heartbeat = False return status elif success_code == 91100: status = '文件接收中。。。' self.recv_code = 91100 self.is_send_heartbeat = False return status elif success_code == 9110040: status = '在线9' print("服务器成功接收发送的大文件,服务器计算md5值相同") tip_message = "91100" + "+0~^D" + "文件发送成功" self.my_signal.emit(tip_message) elif success_code == 110041: status = '接收大文件。。' self.is_send_heartbeat = False self.recv_code = 110041 return status else: status = '下线' return status
def recv_file(self): print("进入分片文件接收操作") confirm_code = package_data_2_security(data2byte(6633)) self.tcp_client.send(confirm_code) flag_code = package_data_2_security(data2byte(int(self.number_name))) self.tcp_client.send(flag_code) print("开始接收分片文件信息") # 接受客户端介绍信息的长度 json_data_len_buffer = self.tcp_client.recv(4) json_data_len_buffer_unpackage = unpackage_data_2_security( json_data_len_buffer) json_data_len = byte2data(json_data_len_buffer_unpackage) # 接受介绍信息的内容 json_data_buffer = self.tcp_client.recv(json_data_len) json_data_buffer_unpackage = unpackage_data_2_security( json_data_buffer) print("数据接收完成") # 将介绍信息的内容反序列化 json_data = json.loads(json_data_buffer_unpackage.decode('utf-8')) print("本次介绍信息的内容为:{}".format(json_data)) print("本次file长度为:{}".format(json_data["filesize"])) recv_len = 0 filename = json_data['filename'] filesize = json_data['filesize'] filepath = json_data['filepath'] aim_device = json_data['aim_device'] md5 = json_data['md5'] with open("TempFile/" + filename, 'wb') as f: now_data_number = 0 c_retry = 0 while recv_len < filesize: package_data_buffer = self.tcp_client.recv(1028) package_data_buffer_un = unpackage_data_2_security( package_data_buffer) # print("Data Length {}".format(len(package_data_buffer_un))) if len(package_data_buffer_un) == 1028: file_buffer = package_data_buffer_un[0:1024] data_number = byte2data(package_data_buffer_un[1024:1028]) # print("Now Data Number {}, Data Number {}, ".format(now_data_number, data_number)) if now_data_number == data_number: if filesize - recv_len > buffsize: recv_len += len(file_buffer) f.write(file_buffer) else: end_size = filesize - recv_len recv_len += end_size f.write(file_buffer[0:end_size]) c_retry = 0 self.tcp_client.send( package_data_2_security(data2byte(941000))) now_data_number += 1 else: self.tcp_client.send( package_data_2_security(data2byte(941001))) c_retry += 1 if c_retry == 5: print("error") return new_file_md5 = getmd5("TempFile/" + filename) if new_file_md5 == md5: print("new_file_md5 {}, md5 {}".format(new_file_md5, md5)) confirm_code = package_data_2_security(data2byte(941000)) self.tcp_client.send(confirm_code) else: confirm_code = package_data_2_security(data2byte(941002)) self.tcp_client.send(confirm_code)
def run(self): self.start_net_fun() while True: if self.is_connection: print("{}可以连接到服务器".format(TAG)) # 心跳保持工具 if self.is_send_heartbeat: qmut_tool.lock() print('heartbeat') self.heartbeat() # self.my_signal.emit("2" + "+0~^D" + self.heartbeat()) self.sleep(3) print("当前recv_code{}".format(self.recv_code)) qmut_tool.unlock() # 获取已连接服务端的客户端数量 elif self.send_code == 1001: qmut_tool.lock() self.tcp_client.send( package_data_2_security(data2byte(1001))) confirm_code_buffer = self.tcp_client.recv(4) confirm_code = byte2data( unpackage_data_2_security(confirm_code_buffer)) if confirm_code == 1001: message_len_buffer = self.tcp_client.recv(4) message_len = byte2data( unpackage_data_2_security(message_len_buffer)) message_buffer = self.tcp_client.recv(message_len) message = unpackage_data_2_security(message_buffer) self.tcp_client.send( package_data_2_security(data2byte(1001))) message = self.return_type_flag + "1001" + "+0~^D" + message.decode( 'utf-8') self.my_signal.emit(message) self.send_code = -1 self.is_send_heartbeat = True else: print("获取数据失败") qmut_tool.unlock() print("end 1001================") # 发送文本消息 elif self.send_code == 1011: self.send_text_message() # 发送文件 elif self.send_code == 1100: print(self.send_file_path + " in tools") self.send_file() # 接收 # 接收文本 elif self.recv_code == 91011: self.rece_text_message() # 接收小文件 if self.recv_code == 91100: print("接收小文件{}".format(self.recv_code)) self.recv_code = -1 self.recv_file() self.is_send_heartbeat = True # 接收小文件 if self.recv_code == 110041: print("接收大文件{}".format(self.recv_code)) self.recv_code = -1 self.recv_big_file() self.is_send_heartbeat = True # 网络错误处理 else: print("网络错误") self.start_net_fun() time.sleep(10)
def send_file(self): head_info_len, head_info, file_size = get_file_inform( self.send_file_path, self.send_aim) print(head_info) if head_info_len > 0: # 小文件发送 if file_size < 512000: try: self.tcp_client.send( package_data_2_security(data2byte(1100))) confirm_data = byte2data( unpackage_data_2_security(self.tcp_client.recv(4))) if confirm_data == 91100: head_info_buffer = head_info.encode('utf-8') head_info_buffer = package_data_2_security( head_info_buffer) head_info_len_buffer = data2byte(len(head_info_buffer)) head_info_len_buffer = package_data_2_security( head_info_len_buffer) self.tcp_client.send(head_info_len_buffer) # 这里是4个字节 self.tcp_client.send(head_info_buffer) # 发送报头的内容 s = time.time() counter = 0 with open(self.send_file_path, 'rb') as sf: while True: data = sf.read(1024) data_len = len(data) if data_len == 0: break elif data_len == 1024: data = data + data2byte(counter) elif data_len < 1024: for i in range(data_len, 1024): data += b'0' data = data + data2byte(counter) timeout_counter = 0 while True: try: self.tcp_client.send( package_data_2_security(data)) confirm_data = byte2data( unpackage_data_2_security( self.tcp_client.recv(4))) if confirm_data == 91100: # print(data) # print(len(data)) # print(counter) counter += 1 timeout_counter = 0 break except socket.timeout as e: timeout_counter += 1 print(e.args) print(traceback.format_exc()) print("文件数据发送超时,尝试重新发送") if timeout_counter == 5: return # break success_code_buffer = self.tcp_client.recv(4) success_code = byte2data( unpackage_data_2_security(success_code_buffer)) if success_code == 91100: self.is_send_heartbeat = True self.send_code = -1 print("文件发送成功, 一共耗时 {}".format(time.time() - s)) tip_message = "91100" + "+0~^D" + "文件发送成功" self.my_signal.emit(tip_message) else: print("文件发送失败") except Exception as e: print(e.args) print(traceback.format_exc()) # 大文件发送 else: try: self.tcp_client.send( package_data_2_security(data2byte(11004))) confirm_data = byte2data( unpackage_data_2_security(self.tcp_client.recv(4))) if confirm_data == 911004: head_info_buffer = head_info.encode('utf-8') head_info_buffer = package_data_2_security( head_info_buffer) head_info_len_buffer = data2byte(len(head_info_buffer)) head_info_len_buffer = package_data_2_security( head_info_len_buffer) self.tcp_client.send(head_info_len_buffer) # 这里是4个字节 self.tcp_client.send(head_info_buffer) # 发送报头的内容 except Exception as e: print(e.args) print("============") print(traceback.format_exc()) for one_file_path in split_file(self.send_file_path, 5): file_send_thread = FileSendQThread(one_file_path, self.host, self.port, self.send_aim) file_send_thread.start() self.send_code = -1 self.send_file_path = '' self.send_aim = '' self.is_send_heartbeat = True