Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

windows - Python Connect over HTTP proxy with pysftp

Currently, I am doing SFTP transfer using Python subprocess.POPEN and PuTTY psftp.exe.

It is working, but not really clean nor transportable.

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:

  • Server IP : 123.123.123.255
  • Server Port : 22
  • Connection Type : SSH
  • AutoLogin UserName : MyUser
  • Proxy type : HTTP
  • Proxy Hostname : gw.proxy.fr
  • Proxy port : 1234
  • Proxy Username : ProxyUser
  • Proxy Password: ProxyPass

How should I input all these parameters in pysftp so I can retrieve my files?

EDIT: 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)

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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('.')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...