def download_request(self, download_path, filename): CODE_NUM = "1" my_protocol.dwld_bale_TCP(self.s, '', filename) # 等待接收 data = my_protocol.unpake_TCP(self.s) if data != -1: if data[2] == '2': # print("文件在服务器里不存") CODE_NUM = '2' elif data[2] == 'go': file_path = download_path + '/' + filename DATA_HOST = self.s.getsockname()[0] DATA_PORT = 0 DATA_ADDR = (DATA_HOST, DATA_PORT) data_socket = socket() data_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) #客户端监听套接字 data_socket.bind(DATA_ADDR) # data_socket.listen(10) data_addr = data_socket.getsockname() #给服务端发送端口号 my_protocol.upld_bale_TCP(self.s, '', str(data_socket.getsockname()[1])) #等待服务端连接 data_socket.close() t = Thread(target=client_data_port.run, args=('d', data_addr, file_path, self.view_handler)) t.setDaemon(True) t.start() # if R==10: # CODE_NUM=10 # else: # CODE_NUM=11 return CODE_NUM
def upload_request(self, file_property, filename): #初始化错误码用于反馈结果 CODE_NUM = '1' # 打包发送 my_protocol.upld_bale_TCP(self.s, file_property, filename) # 等待接收 #生成file对象传给副进程使用 file = File() file.unpack(file_property) data = my_protocol.unpake_TCP(self.s) if data != -1: if data[2] == '3': CODE_NUM = '3' elif data[2] == 'go': DATA_HOST = self.s.getsockname()[0] DATA_PORT = 0 DATA_ADDR = (DATA_HOST, DATA_PORT) data_socket = socket() data_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) #客户端监听套接字 data_socket.bind(DATA_ADDR) # data_socket.listen(10) data_addr = data_socket.getsockname() #给服务端发送端口号 my_protocol.upld_bale_TCP(self.s, '', str(data_socket.getsockname()[1])) #等待服务端连接 data_socket.close() # 开辟新的线程,上传文件 t = Thread(target=client_data_port.run, args=('u', data_addr, file, self.view_handler)) t.setDaemon(True) t.start() return CODE_NUM
def get_command(connfd,addr): # 创建客户端通信对象 c_ftp = MyFtp_Server(connfd,addr) while True: # 接受完的data判断是否丢包,进行解包处理 l = my_protocol.unpake_TCP(connfd) # 客户端功能选择 if l != -1: # l返回的结构:请求类别 + 属性 + 内容 + 结束符 # l[0] l[1] l[2] l[3] if l[0] == 'list': c_ftp.list(l[2]) elif l[0] == 'upld': c_ftp.receive(l[1], l[2]) elif l[0] == 'dwld': c_ftp.send(l[1], l[2]) elif l[0] == 'chat': # l[2] ismessage c_ftp.chat(l[2]) elif l[0] == 'login': # l[1] 是账号 # l[2] 是密码 c_ftp.login(l[1], l[2]) elif l[0]=='reg': c_ftp.register(l[1],l[2]) elif l[0] == 'quit': c_ftp.quit() else: sys.exit(0)
def receive(self, f_property,filename): #初始化错误码 CODE_NUM=0 # filename = os.path.split(filename)[-1] # 搜索系统是否有这个文件 self.ms = My_Mysql() fd = self.ms.select_file_by_filename(filename) self.ms.close() if fd == None: # 不存在,可以上传,给予客户端回应,客户端接收到此消息后,打开副线程进行主动连接,并回复 #服务端相应端口号,用于数据通信 my_protocol.upld_bale_TCP(self.client,'','go') DATA_HOST = self.addr[0] DATA_PORT = int(my_protocol.unpake_TCP(self.client)[2]) DATA_ADDR = (DATA_HOST, DATA_PORT) t = Thread(target=data_port.run,args=('u',DATA_ADDR,f_property,)) t.setDaemon(True) t.start() # #编写属性 # op_file = SYS_FIlE_PATH+filename # up=File(filename, # os.path.getsize(op_file), # op_file, # time.strftime('%Y-%m-%d %H:%M:%S'), # time.strftime('%Y-%m-%d %H:%M:%S')) # #添加到mysql # self.ms.add_file(up) # #TODO 判断用户是否下载成功,是---》添加日志 # if R==12: # #把用户的操作添加到数据库里的日志 # log=(self.addr,filename,time.strftime('%Y-%m-%d %H:%M:%S'),"上传") # self_ms.add_userlog(log) # #报错代码 10为下载成功 11为下载失败 # CODE_NUM=12 # else: # CODE_NUM=13 # Q.put(CODE_NUM) #chat进程 get就ok else: #文件已存在或重名(覆盖操作可以在这) # 返回不存在的代码 CODE_NUM='3' #属性为空,返回报错代码 my_protocol.upld_bale_TCP(self.client,'',CODE_NUM)
def send(self, f_property,filename): CODE_NUM=0 # 如果需要上传下载,开辟新线程进行处理 # 搜索系统是否有这个文件 self.ms = My_Mysql() fd = self.ms.select_file_by_filename(filename) self.ms.close() if fd == None: # 返回不存在的代码 CODE_NUM='2' #属性为空,返回报错代码 my_protocol.dwld_bale_TCP(self.client,'',CODE_NUM) else: my_protocol.upld_bale_TCP(self.client,'','go') #建立套接字 DATA_HOST = self.addr[0] DATA_PORT = int(my_protocol.unpake_TCP(self.client)[2]) DATA_ADDR = (DATA_HOST, DATA_PORT) t = Thread(target=data_port.run, args=('d', DATA_ADDR,filename,)) t.setDaemon(True) t.start()
def run(op,data_addr,file,view_handeler): # TODO:创建TCP套接字,注意将端口设为0即让系统随机分配端口 #为了防止服务器端端口被占用,所以需要不断产生套接字直到端口可使用为止 data_socket = socket() data_socket.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) #客户端监听套接字 data_socket.bind(data_addr) data_socket.listen(5) s,addr = data_socket.accept() #通过套接字做相应处理 print('data_port connected from',addr) data_socket.close() if op=='u': try: #获取文件路径 op_path=file.get_server_path()+'/'+file.get_name() total_size = int(file.get_size()) current_size =0 with open(op_path,'rb') as f: while True: data=f.read(4096) if not data: break s.send(data) current_size +=len(data) view_handeler.show_progress(current_size,total_size) except Exception as E: print(E) #如果出错向前端句柄返回错误码'14' CODE_NUM='14' view_handeler.do_message(CODE_NUM) else: #防止粘包 time.sleep(0.1) s.send(b'upld+ + +@end') ask = my_protocol.unpake_TCP(s)[2] #print(ask) if ask=='ok': CODE_NUM='0' view_handeler.do_message(CODE_NUM,filename = file.get_name()) view_handeler.do_list() else: print('上传失败') CODE_NUM="13" view_handeler.do_message(CODE_NUM) finally: s.close() elif op=='d': try: #接收文件(用户的下载) #路径选择 with open(file,'wb') as f: while True: data=s.recv(4096) if data==b'@end': break f.write(data) except Exception as E: print(E) CODE_NUM='12' s.send(b'12') view_handeler.do_message(CODE_NUM) else: time.sleep(0.1) #下载完成发送ok s.send(b'ok') #返回结果给进程 finally: CODE_NUM = s.recv(1024).decode() if CODE_NUM =='11': view_handeler.do_message(CODE_NUM) else: view_handeler.do_message('4',filename = file.split('/')[-1]) # print('下载完成') s.close()