def download_file(self, source_file_path, target_file_path): from cfg import custom_log download_flag = False ftp = self.__login() try: with open(target_file_path, 'wb') as file_handler: ftp.retrbinary("RETR %s" % source_file_path, file_handler.write, self.__buff_size) custom_log.log_info_cmd('Download File:%s From:%s Success.' % (source_file_path, self.name)) download_flag = True except error_perm: custom_log.log_error_cmd(error_perm) custom_log.log_error_cmd('[Error]Download File:%s From:%s Fail!' % (source_file_path, self.name)) finally: ftp.set_debuglevel(0) ftp.quit() return download_flag
def upload_file(self, source_file_path, target_file_path): from cfg import custom_log upload_flag = False ftp = self.__login() try: with open(source_file_path, 'rb') as file_handler: ftp.storbinary('STOR ' + target_file_path, file_handler, self.__buff_size) custom_log.log_info_cmd('Upload File:%s To:%s Success.' % (source_file_path, self.name)) upload_flag = True except error_perm: custom_log.log_error_cmd(error_perm) custom_log.log_error_cmd('[Error]Upload File:%s To:%s Fail!' % (source_file_path, self.name)) finally: ftp.set_debuglevel(0) ftp.quit() return upload_flag
def remove(self, path): rmdir_flag = False t, sftp = self.__login() try: try: sftp.chdir(path) for file_name in sftp.listdir(path): self.remove('%s/%s' % (path, file_name)) sftp.rmdir(path) except: sftp.remove(path) rmdir_flag = True except Exception as e: print e from cfg import custom_log custom_log.log_error_cmd('Clear Path:%s Fail!' % path) finally: if t is not None: t.close() return rmdir_flag
def run_cmd_str(self, cmd_str): from cfg import custom_log custom_log.log_info_cmd('Server[%s],Cmd[%s] Start.' % (self.name, cmd_str)) cmd_result_list = [] try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(self.ip, self.port, self.user, self.pwd, timeout=90) stdin, stdout, stderr = ssh.exec_command(cmd_str) error_message_list = [] cmd_result = stderr.read() for item in cmd_result.splitlines(): error_message_list.append(item) if len(error_message_list) > 0: error_message_str = '\n'.join(error_message_list) custom_log.log_debug_cmd( '-----------[Server:%s,stderr]-----------' % self.name) if 'Traceback' in error_message_str: error_message = 'Server[%s],Cmd[%s] Error!Error_message:%s' % \ (self.name, cmd_str, error_message_str) custom_log.log_error_cmd(error_message) raise Exception(error_message) cmd_result = stdout.read() for item in cmd_result.splitlines(): cmd_result_list.append(item) if len(cmd_result_list) > 0: custom_log.log_debug_cmd( '-----------[Server:%s,stdout]-----------' % self.name + '\n' + '\n'.join(cmd_result_list)) ssh.close() except Exception: error_msg = traceback.format_exc() error_message = 'Server[%s],Cmd[%s] Exception!Exception_message:%s' % ( self.name, cmd_str, error_msg) custom_log.log_error_cmd(error_message) raise Exception(error_message) custom_log.log_info_cmd('Server[%s],Cmd[%s] Stop!' % (self.name, cmd_str)) return '\n'.join(cmd_result_list)
def remove(self, path): rmdir_flag = False t = paramiko.Transport((self.ftp_ip, self.ftp_port)) t.connect(username=self.ftp_userName, password=self.ftp_passWord) sftp = paramiko.SFTPClient.from_transport(t) try: try: sftp.chdir(path) for file_name in sftp.listdir(path): self.remove('%s/%s' % (path, file_name)) sftp.rmdir(path) except: sftp.remove(path) rmdir_flag = True except Exception as e: print e from cfg import custom_log custom_log.log_error_cmd('Clear Path:%s Fail!' % path) finally: if t is not None: t.close() return rmdir_flag
def __reset_parameter(self): self.reserve_flag = False if hasattr(self, 'server_item') and \ not (self.server_item.ip_reserve is None or self.server_item.ip_reserve == ''): self.ip = self.server_item.ip self.port = self.server_item.port self.db_ip = self.server_item.db_ip self.db_port = self.server_item.db_port # 如果无法连接则切换至备用线路 if not super(TradeServerModel, self).check_connect(): self.reserve_flag = True self.ip = self.server_item.ip_reserve self.port = self.server_item.port_reserve self.db_ip = self.server_item.db_ip_reserve self.db_port = self.server_item.db_port_reserve from cfg import custom_log custom_log.log_error_cmd( 'Server[%s] Change To Reserve IP[%s]' % (self.name, self.ip)) self.__ftp_model = SftpModel(self.name, self.ip, self.port, self.user, self.pwd)
def remove(self, path): rmdir_flag = False ftp = self.__ftp_login() try: ftp.cwd(path) files = ftp.nlst() for file_name in files: self.remove('%s/%s' % (path, file_name)) ftp.rmd(path) rmdir_flag = True except error_perm: try: ftp.delete(path) rmdir_flag = True except Exception as e: print e from cfg import custom_log custom_log.log_error_cmd('Clear Path:%s Fail!' % path) finally: ftp.set_debuglevel(0) ftp.quit() return rmdir_flag
def is_exist(self, path): is_exist_flag = False ftp = self.__login() try: ftp.cwd(path) is_exist_flag = True except error_perm: file_size = 0 try: file_size = ftp.size(path) except error_perm: pass if file_size > 0: is_exist_flag = True else: from cfg import custom_log custom_log.log_error_cmd('FTP Not Exist:%s' % path) finally: ftp.set_debuglevel(0) ftp.quit() return is_exist_flag