0

I would like to connect to a SFTP server via a Http-Proxy with python.

I was able to modify the solution to the following question my favor in order to get a positive response from the Proxy. (url=https://stackoverflow.com/questions/10488832/how-to-ssh-over-http-proxy-in-python-paramiko)

import socket
import paramiko

# PROXY
P_HOST = "P_HOST"
P_PORT = 1234
P_USER = "P_USER"
P_PW = "P_PW"

# SFTP
HOST = "HOST"
PORT = 12
USER = "USER"
PW = "PW"

def http_proxy_tunnel_connect():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(50)
    sock.connect((P_HOST, P_PORT))

cmd_connect = "CONNECT {}:{} HTTP/1.1\r\nProxy-Authorization: {}\r\n\r\n".format(*(P_HOST, P_PORT, f'Basic {P_USER}:{P_PW}')).encode("utf-8")

sock.sendall(cmd_connect)
response = []
# in worst case this loop will take 2 seconds if not response was received (sock.timeout)
while True:
    chunk = sock.recv(1024)
    if not chunk:  # if something goes wrong
        break
    response.append(chunk)
    if "\r\n\r\n".encode("utf-8") in chunk:  # we do not want to read too far ;)
        break

print(f"response: {response}")
return sock


if __name__ == "__main__":
    sock = http_proxy_tunnel_connect()
    # ... do connect via paramiko

I am stuck at the point where I would (probably?) connect with the help of paramiko-library to the sftp server.

I would highly appreciate help at this point.

Best & thanks

echti123
  • 71
  • 1
  • 2

0 Answers0