python编写服务器上控制文件夹_我如何使用python打开远程服务器文件夹?

要通过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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值