Python Connect over HTTP proxy with pysftp

别说谁变了你拦得住时间么 提交于 2019-12-05 13:56:09

I do not think that the pysftp supports proxies. Though note that the pysftp is just a wrapper around Paramiko library, which does support proxies.

So I suggest you to use Paramiko directly.

For a start see How to ssh over http proxy in Python?, particularly the answer by @tintin.


To authenticate to the proxy, after the CONNECT command, add a Proxy-Authorization header like:

Proxy-Authorization: Basic <credentials>

where the <credentials> is base-64 encoded string username:password.

cmd_connect = "CONNECT {}:{} HTTP/1.1\r\nProxy-Authorization: Basic <credentials>\r\n\r\n".format(*target)

In my case, I do this:

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('.')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!