1

I need to extract a file from an FTP server using Python. I have tried to use ftplib, but it does not seem to work when using a proxy. I have also tried using the ftp-proxy-client package it does not seem to work either.

I have used these credentials.

ftp_adress = "XXX.XXX.XXX.XX"
ftp_user = "FTP_user"
ftp_password = "FTP_password"
ftp_port = "XXX"
ftp_path = "path"
file_name = "file_name.xlsx"
local_path = "local_path/file_name.xlsx"
proxy = "proxy_name.com"
proxy_port = "XXXX"

The code I have tried is:

from ftp_proxy_client import FtpProxy

ftp_proxy = FtpProxy(host=proxy, port=proxy_port)

ftp_client = ftp_proxy.connect('ftp_adress', 
                               port=ftp_port, 
                               login=ftp_user, 
                               password=ftp_password)

fp = ftp_client.download(path=f"{ftp_path}/{file_name}")
with open(local_path, 'wb') as file:
    file.write(fp.read())

But I end up with this error:

FtpProxyError: Failed connecting to proxy server

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
frmzi
  • 11
  • 1

1 Answers1

1

Install PySocks and use its set_default_proxy:

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, ip, port)

From there, you use a plain ftplib code, as if you were connecting to the the FTP server directly.

See How to make python Requests work via socks proxy.

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846