如何使用python打开远程服务器文件夹? [英] How I open remote server folder using python?

查看:1938
本文介绍了如何使用python打开远程服务器文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打开远程服务器文件夹> 在仅图像存储的文件夹中,我们读取了所有图像.

How to Open the remote server folder > inside the folder only images store we read all the images.

服务器是Linux服务器

Server is Linux server

import paramiko
import sys
import os

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('<server-Ip-address>', username='******', password='******')

ftp = ssh.open_sftp()
#filea = ftp.get('/var/www/folder_image/', '#')
#Here coded how we open the dir and read one by one all images property(name,size,path,etc.)
ftp.close()

我使用了这段代码

所以请回复

推荐答案

要通过ssh从远程文件夹下载所有文件,可以使用ftp.listdir()列出文件,然后为每个文件列出ftp.get():

To download all files from a remote folder over ssh, you could use ftp.listdir() to list the files followed by ftp.get() for each file:

#!/usr/bin/env python
import os
import sys
from contextlib import closing

from paramiko import SSHConfig, SSHClient

# specify hostname to connect to and the remote/local paths
hostname, remote_dirname, destdir = sys.argv[1:]

# load parameters to setup ssh connection
config = SSHConfig()
with open(os.path.expanduser('~/.ssh/config')) as config_file:
    config.parse(config_file)
d = config.lookup(hostname)

# connect
with closing(SSHClient()) as ssh:
    ssh.load_system_host_keys() #NOTE: no AutoAddPolicy() 
    ssh.connect(d['hostname'], username=d.get('user'))
    with closing(ssh.open_sftp()) as sftp:
        # cd into remote directory
        sftp.chdir(remote_dirname)
        # cd to local destination directory
        os.chdir(destdir)
        # download all files in it to destdir directory
        for filename in sftp.listdir():
            sftp.get(filename, filename)

这篇关于如何使用python打开远程服务器文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆