Setup a Static IP for Python FTP

Learn how to route Python FTP transfers through a QuotaGuard Static IP. Includes code examples.

Prerequisites

pip install pyftplib PySocks

Instructions

Run example
QUOTAGUARDSTATIC_URL=... python app.py

Be sure to set QUOTAGUARDSTATIC_URL to your proxy URL from the QuotaGuard Dashboard. Either the HTTP or the SOCKS URL will work because the program will convert to the right port number.

Test in Docker
docker build -t qg-static-python-ftp-example .
docker run -e QUOTAGUARDSTATIC_URL=... qg-static-python-ftp-example

Code Samples

app.py
#/usr/bin/env python
import ftplib
import os
import socks

from urllib.parse import urlparse

if __name__ == '__main__':
    quotaguard = urlparse(os.environ['QUOTAGUARDSTATIC_URL'])
    proxy = socks.socksocket()
    proxy.set_proxy(
        proxy_type=socks.SOCKS5,
        addr=quotaguard.hostname,
        port=1080,
        username=quotaguard.username,
        password=quotaguard.password,
    )
    proxy.connect(('speedtest.tele2.net', 21))

    ftp = ftplib.FTP()
    ftp.sock = proxy
    ftp.connect('speedtest.tele2.net', 21)
    ftp.login('anonymous', '')

    filename = '1KB.zip'

    with open(filename, 'wb') as local_file:
        ftp.retrbinary('RETR ' + filename, local_file.write)

    if os.path.exists(filename):
        print('File downloaded successfully')
        os.remove(filename)

    ftp.quit()
    proxy.close()