python win+r时不成功,来自Linux的Python pysftp get_r在Linux上工作正常,但在Windows上工作不正常...

I would like to copy an entire directory structure with files and subfolders recursively using SFTP from a Linux server to a local machine (both Windows and Linux)using Python 2.7.

I am able to ping the server and download the files using WinSCP from the same machine.

I tried the following code, works fine on Linux but not on Windows.

I tried \, /, os.join, all gives me same error, checked permissions as well.

import os

import pysftp

cnopts = pysftp.CnOpts()

cnopts.hostkeys = None # disable host key checking.

sftp=pysftp.Connection('xxxx.xxx.com', username='xxx',password='xxx',cnopts=cnopts)

sftp.get_r('/abc/def/ghi/klm/mno', 'C:\pqr',preserve_mtime=False)

File "", line 1, in File "C:\Python27\lib\site-packages\pysftp_init_.py", line 311, in get_r preserve_mtime=preserve_mtime)

File "C:\Python27\lib\site-packages\pysftp_init_.py", line 249, in get self._sftp.get(remotepath, localpath, callback=callback)

File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 769, in get with open(localpath, 'wb') as fl: IOError: [Errno 2] No such file or directory: u'C:\\pqr\\./abc/def/ghi/klm/mno/.nfs0000000615c569f500000004'

解决方案

Indeed, pysftp get_r does not work on Windows. It uses os.sep and os.path functions for remote SFTP paths, what is wrong, as SFTP paths always use a forward slash.

But you can easily implement a portable replacement:

import os

from stat import S_ISDIR, S_ISREG

def get_r_portable(sftp, remotedir, localdir, preserve_mtime=False):

for entry in sftp.listdir_attr(remotedir):

remotepath = remotedir + "/" + entry.filename

localpath = os.path.join(localdir, entry.filename)

mode = entry.st_mode

if S_ISDIR(mode):

try:

os.mkdir(localpath)

except OSError:

pass

get_r_portable(sftp, remotepath, localpath, preserve_mtime)

elif S_ISREG(mode):

sftp.get(remotepath, localpath, preserve_mtime=preserve_mtime)

Use it like:

get_r_portable(sftp, '/abc/def/ghi/klm/mno', 'C:\\pqr', preserve_mtime=False)

Possible modifications of the code:

Side note: Do not "disable host key checking". You are losing a protection against MITM attacks.

For a correct solution, see Verify host key with pysftp.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值