1

I am new to python paramiko. I am trying to upload files in my client server with sftp using python-paramiko. I have code without proxy setup. Please find that below.

host = "abc.mno.com"
port = 22
proxy_uri = "http://username:password@proxyIP:port"
url = urlparse(proxy_uri)
http_con = http.client.HTTPConnection(url.hostname, url.port)
print(url.hostname)
http_con.trust_env=False
headers = {}
if url.username and url.password:
    auth = '%s:%s' % (url.username, url.password)
    headers['Proxy-Authorization'] = 'Basic '+auth
    print(headers)
    http_con.set_tunnel(host, port, headers)
    http_con.connect()
    sock = http_con.sock    client = paramiko.SSHClient()
    client.connect(
        hostname=host,
        port=port,
        username='username',
        key_filename="E:\\keyfile.pem",
        sock=sock,
        )
    print('connected')

I am trying this in windows server. When I execute the program, I am getting error stating "OSError: Tunnel connection failed: 407 Proxy Authentication Required". How can I authenticate the proxy. Could anyone please help me to connect to sftp with proxy(http) setup.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • *"I just want to use proxy in the python script alone"* – That's what the answer by @tintin to the duplicate question does, isn't it? – Martin Prikryl Oct 05 '20 at 10:23
  • @MartinPrikryl, I cannot install tinyproxy in windows(@tintin mentioned this as step 1). Also, proxy which I am using has username and password. I am totally new to this paramiko and proxy. – Karthi k'yan Oct 05 '20 at 10:44
  • The tinyproxy is mentioned there as a **test proxy** against which the code was tested. It's not part of the solution. – *"all you need is any proxy that allows you to CONNECT to your ssh port".* – Martin Prikryl Oct 05 '20 at 10:48
  • got it. Thanks. I tried the below code for proxy setup. I am getting error stating "OSError: Tunnel connection failed: 407 Proxy Authentication Required". How can I provide proxy authentication. – Karthi k'yan Oct 06 '20 at 05:47
  • Check the new link I've added to the list of duplicates. – Martin Prikryl Oct 06 '20 at 06:01
  • I have used Proxy-Authorization and encoded it as mentioned in that link. Still I am getting same error(OSError: Tunnel connection failed: 407 Proxy Authentication Required). – Karthi k'yan Oct 07 '20 at 07:41
  • I do not see any base64 encoding in your code. – Martin Prikryl Oct 07 '20 at 07:42
  • Also, when I print headers, I am getting "{'Proxy-Authorization':b'Abcederfghijklmnopqrsty='}". Is this the correct format? – Karthi k'yan Oct 07 '20 at 07:48
  • for encoding, I have changed the below line. headers['Proxy-Authorization'] = base64.b64encode(('Basic '+ auth).encode()) – Karthi k'yan Oct 07 '20 at 07:50
  • No it's not. As my answer says, you encode only the credentials, not the `Basic` prefix. – Martin Prikryl Oct 07 '20 at 08:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222645/discussion-between-karthi-kyan-and-martin-prikryl). – Karthi k'yan Oct 07 '20 at 08:41

0 Answers0