python - 我如何使用 python 打开远程服务器文件夹?

标签 python linux ssh openerp

如何打开远程服务器文件夹> 在文件夹中只有图像存储我们读取所有图像。

服务器是Linux服务器

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() :

#!/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 - 我如何使用 python 打开远程服务器文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20286745/

相关文章:

linux - 检测读取器何时关闭命名管道 (FIFO)

perl - 通过ssh将stdin/stdout重定向到远程主机/从远程主机重定向

ssh - 网络鸭错误 "Login Failed"

python - 使用 Python 发送和解析 HTTP POST 请求

python - 从url列表中提取html代码

python - 如何 'unpack' pandas数据框列

c# - 如何在C#中将密码字符串转换为Renci.SshNet.PrivateKeyFile?

python - WT 表格 : how do I set a default value for an HTML5 Widget?

Linux命令行图形

linux - 我需要 linux 中的所有名称服务器 (DNS)