使用 pysftp 通过 HTTP 代理进行 Python 连接 [英] Python Connect over HTTP proxy with pysftp
问题描述
目前,我正在使用 Python subprocess.POPEN
和 PuTTY psftp.exe
进行 SFTP 传输.
Currently, I am doing SFTP transfer using Python subprocess.POPEN
and PuTTY psftp.exe
.
它正在工作,但不是很干净,也不是很便携.
It is working, but not really clean nor transportable.
我想使用 Python pysftp 重现相同的行为,但我不知道在哪里输入所有参数.我在 PuTTY 中有以下配置:
I would like to reproduce the same behavior using Python pysftp, but I do not know where to input all the parameters. I have in PuTTY the following configuration:
- 服务器IP:123.123.123.255
- 服务器端口:22
- 连接类型:SSH
- 自动登录用户名:MyUser
- 代理类型:HTTP
- 代理主机名:gw.proxy.fr
- 代理端口:1234
- 代理用户名:ProxyUser
- 代理密码:ProxyPass
我应该如何在 pysftp 中输入所有这些参数以便我可以检索我的文件?
How should I input all these parameters in pysftp so I can retrieve my files?
使用 Martin Prikryl 的答案,我发现了一些新的东西可以探索.如果我理解得很好,我需要使用套接字.把我输入我需要的所有信息仍然有一些问题.
Using the answer from Martin Prikryl, I found some new stuffs to explore. If I understand well, I need to use a socket. Put I still have some problem to input all the information I need.
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
proxy = ("gw.proxy.fr",1234)
sock.connect(proxy)
target=("123.123.123.255",23)
cmd_connect = "CONNECT {}:{} HTTP/1.1
".format(*target)
sock.sendall(cmd_connect)
我收到的响应是HTTP/1.0 407 Proxy Authentication Required
,这很正常,因为我没有在任何地方使用代理身份验证信息.那么,您知道我如何使用它们并将它们输入到我的套接字中吗?
The respond that I receive from this is HTTP/1.0 407 Proxy Authentication Required
, which is kind of normal because I did not use the Proxy authentication information anywhere. So, do you have any idea how I can use them and input them into my socket ?
推荐答案
我认为 pysftp 不支持代理.但请注意,pysftp 只是对 Paramiko 库 的封装,它确实支持代理.
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.
所以我建议你直接使用Paramiko.
So I suggest you to use Paramiko directly.
首先请参阅如何在 Python Paramiko 中通过 HTTP 代理进行 ssh?,尤其是 @tintin 的回答.
For a start see How to ssh over HTTP proxy in Python Paramiko?, particularly the answer by @tintin.
要对代理进行身份验证,在 CONNECT
命令之后,添加一个 Proxy-Authorization
标头,例如:
To authenticate to the proxy, after the CONNECT
command, add a Proxy-Authorization
header like:
Proxy-Authorization: Basic <credentials>
其中
是 base-64 编码的字符串 username:password
.
where the <credentials>
is base-64 encoded string username:password
.
auth = 'Basic ' + base64.encodebytes("username:password".encode()).decode()
args = ("123.123.123.255", 23, auth)
cmd_connect = "CONNECT {}:{} HTTP/1.1
Proxy-Authorization: {}
".format(*args)
这篇关于使用 pysftp 通过 HTTP 代理进行 Python 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!