def fpwd(self,command): '''check the current directory in the server''' send_msg = message.encode_msg_str(PWD,0,'') self.client.sendall(send_msg) rcv_msg = self.__read_as_bytearray() dict_msg = message.decode_msg(rcv_msg) print('in the server , the current work directory is ' + dict_msg['server_current_dir_item'])
def pack_get(self,command): arg_list = command.split() pack_file_name = arg_list[1] for file in os.listdir(current_dir): if pack_file_name == file: print("redundant file name ,eror") return send_msg = message.encode_msg_str(PAC_GET,0,pack_file_name) self.client.sendall(send_msg) rcv_msg = self.__read_as_bytearray() dict_msg = message.decode_msg(rcv_msg) print(rcv_msg) status_code = dict_msg['status_code'] if status_code == NO_DIR_FILE: print('no such dir or file') return file_size = dict_msg['get_file_size'] zipName = pack_file_name + '_tmp.zip' f = open(current_dir + '\\' + zipName,'wb') f.write(dict_msg['file_byte']) rcv_file_size = dict_msg['transfer_byte_length'] #print('file_size: '+ str(file_size))#test while rcv_file_size < file_size: rcv_msg = self.__read_as_bytearray() dict_msg = message.decode_msg(rcv_msg) status_code = dict_msg['status_code'] if status_code == ON_TRANSFER_GET: transfer_byte_length = dict_msg['transfer_byte_length'] file_byte = rcv_msg[5:5+transfer_byte_length] f.write(file_byte) #print('rcv_file_size: '+ str(rcv_file_size))#test rcv_file_size += transfer_byte_length else: break send_msg = message.encode_msg_str(SUCC_GET,0,'') self.client.sendall(send_msg) f.close() packDir.unzipDirectory(current_dir,zipName) #print('current_dir: ' + str(current_dir))#test #print('zipName: ' + str(zipName))#test os.remove(zipName)
def fcd(self,command): '''shift to other directory in the server''' arg_list = command.split() cd_dir_name = arg_list[1] send_msg = message.encode_msg_str(CD,0,cd_dir_name) self.client.sendall(send_msg) rcv_msg = self.__read_as_bytearray() dict_msg = message.decode_msg(rcv_msg) if dict_msg['status_code'] == NO_DIR_FILE: print('no such file or directory')
def fdir(self,command): '''check what files the current directory in the server has''' send_msg = message.encode_msg_str(DIR,0,'') self.client.sendall(send_msg) rcv_msg = self.__read_as_bytearray() dict_msg = message.decode_msg(rcv_msg) output_string = '' for file_item in dict_msg['dir_result']: output_string += file_item + ' ' print(output_string)
def fget(self,command): '''get file in the server''' arg_list = command.split() get_file_name = arg_list[1] for file in os.listdir(current_dir): if file == get_file_name: print("redundant file name ,eror") return send_msg = message.encode_msg_str(GET,0,get_file_name) self.client.sendall(send_msg) rcv_msg = self.__read_as_bytearray() dict_msg = message.decode_msg(rcv_msg) status_code = dict_msg['status_code'] if status_code == NO_DIR_FILE: print('no such dir or file') return file_size = dict_msg['get_file_size'] f = open(current_dir + '\\' + get_file_name,'wb') f.write(dict_msg['file_byte']) rcv_file_size = dict_msg['transfer_byte_length'] #print('file_size: ' + str(file_size))#test while rcv_file_size < file_size: rcv_msg = self.__read_as_bytearray() dict_msg = message.decode_msg(rcv_msg) status_code = dict_msg['status_code'] if status_code == ON_TRANSFER_GET: transfer_byte_length = dict_msg['transfer_byte_length'] file_byte = rcv_msg[5:5+transfer_byte_length] f.write(file_byte) #print('rcv_file_size: ' + str(rcv_file_size))#test rcv_file_size += transfer_byte_length else: break send_msg = message.encode_msg_str(SUCC_GET,0,'') self.client.sendall(send_msg) f.close()
def interactive(self): '''begin to interactive''' while True: command = input("--> ").strip() if not command:continue if command == "exit": send_msg = message.encode_msg_str(EXIT,0,'') self.client.sendall(send_msg) self.client.close() sys.exit(0) break command_name = command.split()[0] if hasattr(self,command_name): func = getattr(self,command_name) func(command) else:print("no command has the name you input")
def fmkdir(self,command): '''create a new directory''' arg_list = command.split() mkdir_name = arg_list[1] send_msg = message.encode_msg_str(MKDIR,0,mkdir_name) self.client.sendall(send_msg)
serversocket.listen(1024) login = False while True: connection, (addr, port) = serversocket.accept() if login == False: print("Server connected to " + addr) running = True login_info = read_as_bytearray(connection) username, password, parse_err = parse_usr_info(login_info) if parse_err: print("the first msg is not about login") connection.close() break check_msg, login = check_user_db(username, password) login_recv_msg = message.encode_msg_str(SUCC_LOGIN, 0, current_work_dir) if login: connection.sendall(login_recv_msg) print(username + " login") else: login_recv_msg = message.encode_msg_str(ERR_LOGIN, 0, check_msg) connection.sendall(login_recv_msg) print(username + " tried to login but failed due to " + check_msg) connection.close() break while login: cmd_rcv = read_as_bytearray(connection) dict_msg = message.decode_msg(cmd_rcv) status_code = dict_msg['status_code']