def copyFileToComputer(ftp_client: paramiko.SFTPClient, remoteFolder: str, resFolder: str, filename: str) -> pref.Error: ''' Copies a file to a remote computer ftp_client @param ftp_client:parmiko.STFPClient the ssh ftp connection what the file will be transferred over @param remoteFolder:str ''' err = pref.Success #creates folder if it doesn't exist try: ftp_client.mkdir(remoteFolder, mode=0o777) ftp_client.chmod(remoteFolder, mode=0o777) except: #This is happen if the folder already exists . pass #copies file try: ftp_client.put("{}{}".format(resFolder, filename), "{}{}".format(remoteFolder, filename)) except Exception as e: print(e) #couldn't find script on sever. err = pref.getError(pref.ERROR_FILE_NOT_FOUND, args=(resFolder + filename)) logger.error(err) try: ftp_client.chmod("{}{}".format(remoteFolder, filename), 0o777) except: pass if (ftp_client): ftp_client.close() return err
def makedirs(sftp: SFTPClient, remote_dir): if exists(sftp, remote_dir): return # check parent dir pp = Path(remote_dir).parent.as_posix() if exists(sftp, pp): sftp.mkdir(remote_dir) else: makedirs(sftp, pp) sftp.mkdir(remote_dir)
def makedirs(sftp_client: paramiko.SFTPClient, remotedir: str, mode="777"): if isdir(sftp_client, remotedir): pass elif isfile(sftp_client, remotedir): raise OSError( f"a file with the same name as the remotedir {remotedir} already exists" ) else: head, tail = posixpath.split(remotedir) if head and not isdir(sftp_client, head): makedirs(sftp_client, head, mode) if tail: sftp_client.mkdir(remotedir, mode=int(mode, 8))
def empty(sftp: SFTPClient, path): if not SystemUtils.isdir(sftp, path): sftp.mkdir(path) files = sftp.listdir(path=path) for f in files: file_path = os.path.join(path, f) if SystemUtils.isdir(sftp, file_path): SystemUtils.empty(sftp, file_path) else: sftp.remove(file_path)
def sftp_chdir(sftp: paramiko.SFTPClient, remote_directory: Path): if remote_directory == '/': sftp.chdir('/') return if remote_directory == '': return try: sftp.chdir(str(remote_directory)) except IOError: dirname, basename = os.path.split(str(remote_directory).rstrip('/')) sftp_chdir(sftp, dirname) sftp.mkdir(basename) sftp.chdir(basename) return True
def sftp_mkdir(client: paramiko.SFTPClient, dirpath: str): """Create a directory on the remote server. ---------- client: paramiko.SFTPClient SFTP client. dirpath: string Path to the created directory on the remote server. """ try: # Attempt to change into the directory. This will raise an error # if the directory does not exist. client.chdir(dirpath) except IOError: # Create directory if it does not exist. client.mkdir(dirpath)
def ssh_cpdir(sftp: SFTPClient, local_dir: str, remote_dir: str, cur: str = ''): """ どうやら paramiko には再帰コピー機能がないようなので地道にコピー """ local_cur_dir = path.join(local_dir, cur) remote_cur_dir = remote_dir if '' == cur else remote_dir + '/' + cur sftp.mkdir(remote_cur_dir, 0o755) for child_name in os.listdir(local_cur_dir): child_path = path.join(local_cur_dir, child_name) if path.isdir(child_path): ssh_cpdir(sftp, local_dir, remote_dir, cur + '/' + child_name) else: sftp.put(child_path, remote_cur_dir + '/' + child_name)
def upload_file(sftp: paramiko.SFTPClient, local_path, remote_path) -> paramiko.SFTPClient: local_all = files_of_dir(local_path) for file in local_all: real_remote = os.path.join( remote_path, file.replace(os.path.dirname(local_path), '').lstrip('\\')).replace("\\", "/") remote_p = os.path.dirname(real_remote) # 远程创建文件夹 try: stat = sftp.stat(remote_p) except FileNotFoundError: sftp.mkdir(remote_p) stat = sftp.stat(remote_p) if S_ISDIR(stat.st_mode): # sftp 的 put命令,需要在 remotepath 中,指定文件的名字 # f.put(r'D:\Python.zip', r'/10.130.218.69 sftp (10.130.218.69)/nginx/home/nginx/webapps/Python.zip') sftp.put(file, real_remote) return sftp