简体   繁体   中英

How do I re-create pysftp.Connection with a proxy parameter?

I am connecting to an SFTP server using pysftp but need to reconfigure it to go through a proxy. Since pysftp doesn't support it, I'm thinking of using Paramiko .

Looks like I'm utilizing the benefits of pysftp.Connection, since it looks like my code is using recursive file transfers.

What are the steps I would need to do to re-create pysftp.Connection but with the option to use a proxy? Looking through the codebase is a little frightening since I'm not sure what to edit...

You can do:

import pysftp
import paramiko

hostname, prot = 'some.host.name', 22
proxy = paramiko.proxy.ProxyCommand('/usr/bin/nc --proxy proxy.foobar:8080 %s %d' % (hostname, port))
t = paramiko.Transport(sock=proxy)
t.connect(username='abc', password='123')

sftp = paramiko.SFTPClient.from_transport(t) # back to pysftp wrapper
sftp.listdir('.')

Here's the origin of the code, with some discussion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM