def get_ftp(self): if self.ftp is None: self.ftp = FTPHost(self.hostname, self.username, self.password) self.ftp.chdir(self.path) else: try: self.ftp.chdir(self.path) except Exception as e: self.ftp = FTPHost(self.hostname, self.username, self.password) self.ftp.chdir(self.path) return self.ftp
def autoupdateftp(): ftp_host, ftp_name, ftp_pwd, ftp_path, keep_update_dirs, exclude_dirs, root_dir = read_conf( ) from ftputil import FTPHost from ftputil.error import FTPError from ftputil.error import TemporaryError try: ftp_s = FTPHost(ftp_host, ftp_name, ftp_pwd) except FTPError as e: print('cannot connect ftp sever ---- ' + str(e)) exit(0) if ftp_path != '/': ftp_s.chdir(ftp_path) try: ftp_s.keep_alive() except TemporaryError as e: print('Program execute exception ---- ' + str(e)) exit(0) import os if keep_update_dirs == '/': if exclude_dirs != '': download_file(ftp_s, keep_update_dirs, root_dir, exclude_dirs) else: download_file(ftp_s, keep_update_dirs, root_dir) else: sdirs = keep_update_dirs.split(',') for dir in sdirs: download_file(ftp_s, dir.strip(), root_dir + os.sep + dir.strip()) ftp_s.close()
def get_host(): c = current_app.config session_factory = FTP_TLS if c['FTP_USE_TLS'] else FTP return FTPHost(c['FTP_ADDRESS'], c['FTP_USERNAME'], c['FTP_PASSWORD'], session_factory=session_factory)
def get_ftp(): ftp_path = getattr(settings, 'FTP_PATH', 'ftp://*****:*****@192.168.60.70/soft/测试文件') parsed = urlparse(ftp_path) ftp = FTPHost(parsed.hostname, parsed.username, parsed.password) ftp.chdir(parsed.path) return ftp
def move_nas(local_dir, remote_dir): with FTPHost('10.100.4.102', 'root', 'originp123') as ftp_host: for root, dirs, files in os.walk(local_dir, topdown=False): for name in files: local_file = os.path.join(root, name) remote_file = remote_dir + '/' + name ftp_host.upload(local_file, remote_file) os.remove(local_file)
def run(self): try: with FTPHost(self.hostname, self.username, self.password) as ftp: for folder in ftp.listdir('.'): log.info('Found remote folder: {}'.format(folder)) except Exception as e: print(e)
def install_server_configuration(host, username, password): with FTPHost(host, username, password) as remote: if remote.path.isfile('config_inc.php.bak'): remote.remove('config_inc.php.bak') if remote.path.isfile('config_inc.php'): remote.rename('config_inc.php', 'config_inc.php.bak') remote.upload( os.path.join(os.path.dirname(__file__), "recources/config_inc.php"), 'config_inc.php')
def move_nas(local_dir, remote_dir): with FTPHost('10.100.4.102', 'root', 'originp123') as ftp_host: for root, dirs, files in os.walk(local_dir, topdown=False): for name in files: file_year = root.split('/')[-1] remote_fdir = os.path.join(remote_dir, file_year) if not ftp_host.path.exists(remote_fdir): ftp_host.mkdir(remote_fdir) local_file = os.path.join(root, name) ftp_host.upload(local_file, os.path.join(remote_fdir, name)) os.remove(local_file) for name in dirs: os.rmdir(os.path.join(root, name))
def get_ftputil_client(self): if self.protocol == "sftp": raise TypeError( "ftputil does not manage sftp protocol, please use another ftp client" ) if self.host == "": raise ValueError( "ftp account is not configured, can't initialize ftputil client" ) password = self.get_password() session_factory = ftputil.session.session_factory(port=self.port) return FTPHost(self.host, self.login, password, session_factory=session_factory)
def move_nas(local_dir, remote_dir): fs_reports = search_fs_reports(local_dir) db_reports = search_db_reports(fs_reports) src_files = pick_files(fs_reports, db_reports) with FTPHost('10.100.4.102', 'root', 'originp123') as ftp_host: for local_file in src_files: *_, file_year, filename = local_file.split(os.sep) remote_fdir = os.path.join(remote_dir, file_year) if not ftp_host.path.exists(remote_fdir): ftp_host.mkdir(remote_fdir) ftp_host.upload(local_file, os.path.join(remote_fdir, filename)) os.remove(local_file) # 删除空目录 for it in local_dir: dir_path = os.path.join(local_dir, it) try: os.rmdir(dir_path) except OSError: pass
def _append_todo(start_date, sidecar_dir, ftp_site, ftp_dir, listing, original_dirs_list): try: with FTPHost(ftp_site, 'anonymous', '@anonymous') as ftp_host: dirs = ftp_host.listdir(ftp_dir) for entry in dirs: entry_fqn = '{}/{}'.format(ftp_dir, entry) entry_stats = ftp_host.stat(entry_fqn) if entry_stats.st_mtime >= start_date: if stat.S_ISDIR(entry_stats.st_mode): # True - it's a directory, follow it down later if entry_fqn not in original_dirs_list: listing[entry_fqn] = [True, entry_stats.st_mtime] elif entry.endswith('.fits') or entry.endswith('.fits.gz'): # False - it's a file, just leave it in the list listing[entry_fqn] = [False, entry_stats.st_mtime] logging.debug(f'Adding entry {entry_fqn}') ftp_host.close() temp_listing = {} for entry, value in listing.items(): if value[0] and entry not in original_dirs_list: # is a directory temp_listing.update( _append_todo(start_date, sidecar_dir, ftp_site, entry, temp_listing, original_dirs_list)) _sidecar(entry, value, sidecar_dir) original_dirs_list[entry] = value[1] logging.info(f'Added results for {entry}') _cache(temp_listing, sidecar_dir) listing.update(temp_listing) except Exception as e: logging.error(e) logging.debug(traceback.format_exc()) raise mc.CadcException(f'Could not list {ftp_dir} on {ftp_site}') return listing
def restore_server_configuration(host, username, password): with FTPHost(host, username, password) as remote: if remote.path.isfile('config_inc.php.bak'): if remote.path.isfile('config_inc.php'): remote.remove('config_inc.php') remote.rename('config_inc.php.bak', 'config_inc.php')
if __name__ == "__main__": ftp_host = None try: while True: server = input("Set server address: ") if server == "": server = "192.168.0.81" user = input("User: "******"": user = "******" password = input("Password: "******"": password = "******" try: ftp_host = FTPHost(server, user, password) break except error.PermanentError as e: print(e) actions(ftp_host) except KeyboardInterrupt: print("Force exit...") if ftp_host: ftp_host.close() # names = ftp_host.listdir(ftp_host.curdir) # for name in names: # if ftp_host.path.isfile(name): # ftp_host.download(name, name) # remote, local # # Make a new directory and copy a remote file into it.
external_deps = ['ftputil', 'easysnmp', 'dateutil'] cisco_miburl = urlparse("ftp://ftp.cisco.com/pub/mibs/v2/v2.tar.gz") mibs_dest = '/usr/share/snmp/mibs' try: for dep in external_deps: imp.find_module(dep) except ImportError: deplist = ", ".join(external_deps) print "Unmet dependencies, the following external dependencies are required: {}".format( deplist) from ftputil import FTPHost # noqa print "Downloading required mibs..." with FTPHost(cisco_miburl.netloc, 'anonymous', 'anonymous') as host: if host.path.isfile(cisco_miburl.path): tempfile = mkstemp(suffix='.tar.gz')[1] host.download(cisco_miburl.path, tempfile) print "Unpacking tarball..." tar = tarfile.open(tempfile) tar.extractall() tar.close() os.unlink(tempfile) print "Installing MIBs..." for mib in os.listdir("./auto/mibs/v2"): if not mib.endswith(".my"): continue srcmib = "auto/mibs/v2/{}".format(mib) destmib = "{}/{}".format(mibs_dest, mib) if os.path.exists(destmib):
def ftp(self): """If the data is to be obtained by FTP, here is the ftputil object.""" from ftputil import FTPHost ftp = FTPHost(self.ftp_url, "anonymous") ftp.chdir(self.ftp_dir) return ftp
def run(self): if form.stop_sync is not True: for folder in self.list_of_folders: log.info( 'Attempting to retrieve file list from {}'.format(folder)) try: with FTPHost(self.hostname, self.username, self.password) as ftp: for root, dirs, files in ftp.walk(folder): for file in files: form.list_transfer.addItem(root + '/' + file) except Exception as e: log.exception(e) for i in range(0, form.list_transfer.count()): if form.stop_sync is not True: remote_file = Path(form.list_transfer.takeItem(0).text()) local_file = Path(self.download_path + '/' + str(remote_file)) # create folder for file try: os.chdir(self.download_path) os.makedirs(str(local_file.parent)) log.info('Created local folder: {}'.format( str(local_file.parent))) except FileExistsError as e: log.warning('{} already exists, skipping..'.format( str(local_file.parent))) # enter the local folder try: os.chdir(str(local_file.parent)) except Exception as e: log.exception(e) # enter the remote folder log.info('Attempting to download: {}'.format( str(local_file.parts[-1]))) try: with ftp2(self.hostname, self.username, self.password) as ftp: for i in remote_file.parts[0:-1]: ftp.cwd(str(i)) fh = open(str(local_file), 'wb') ftp.retrbinary( 'RETR {}'.format(str(remote_file.parts[-1])), fh.write) fh.close() log.info('Download Complete: {}'.format( str(local_file))) form.list_finished_transfer.addItem(str(local_file)) except Exception as e: log.exception(e) else: form.list_transfer.clear() form.action_add_folder.setEnabled(True) form.action_download_path.setEnabled(True) form.action_load_settings.setEnabled(True) form.action_save_settings.setEnabled(True) form.action_find_remote_folders.setEnabled(True) form.action_sync.setEnabled(True) form.action_stop_sync.setEnabled(False) form.input_hostname.setEnabled(True) form.input_username.setEnabled(True) form.input_password.setEnabled(True)