[Python] Can't Upload to FTPS using Python's Ftplib

Discussão em 'Python' iniciado por Stack, Julho 30, 2025.

  1. Stack

    Stack Membro Participativo

    I have a script that I want to run daily via Cron that would first download files and upload files from a local directory to FTPS server.

    I have created this script:

    import ftplib
    import ssl
    import socket
    import os
    import io

    # --- Configuration ---
    host = 'XXX' # Replace with actual server
    port = 990 # Implicit FTPS
    username = 'XXX' # Replace with your username
    password = 'XXX' # Replace with your password
    download_from_dir = '/users/Outbound'
    upload_to_dir = '/users/Outbound'
    local_download_dir = './Download' # Change as needed
    local_upload_dir = '/home/ilfs/Public/tals-datasets/2025-07-25'

    BLOCKSIZE = 128 * 1024 # 256 KB

    # --- Ensure local download folder exists ---
    os.makedirs(local_download_dir, exist_ok=True)

    # --- Create SSL context for Implicit FTPS ---
    context = ssl.create_default_context()
    context.check_hostname = False
    context.verify_mode = ssl.CERT_NONE

    # --- Connect and wrap socket manually ---
    sock = socket.create_connection((host, port))
    ssl_sock = context.wrap_socket(sock, server_hostname=host)

    ftp = ftplib.FTP_TLS()
    ftp.sock = ssl_sock
    ftp.af = socket.AF_INET
    ftp.file = io.TextIOWrapper(ssl_sock.makefile('rb'), encoding='utf-8', newline='\r\n')
    ftp.welcome = ftp.getresp()
    ftp.prot_p()
    ftp.login(username, password)
    ftp.set_pasv(True)
    ftp.host = host # override unroutable passive IP

    # ========== DOWNLOAD SECTION ==========
    print(f"\n Connecting to remote download folder: {download_from_dir}")
    ftp.cwd(download_from_dir)
    files = ftp.nlst()
    print(f" Found {len(files)} files to download...\n")

    for fname in files:
    local_path = os.path.join(local_download_dir, fname)
    print(f"⬇ Downloading: {fname}")
    with open(local_path, 'wb') as f:
    ftp.retrbinary(f"RETR {fname}", f.write, blocksize=BLOCKSIZE)
    print("✅ All downloads complete.\n")

    # ========== UPLOAD SECTION ==========
    print(f" Switching to upload directory: {upload_to_dir}")

    local_files = [f for f in os.listdir(local_upload_dir) if os.path.isfile(os.path.join(local_upload_dir, f))]

    ftp.cwd(upload_to_dir)
    ftp.set_pasv(True)
    ftp.host = host

    for fname in local_files:
    local_path = os.path.join(local_upload_dir, fname)
    if os.path.isfile(local_path):
    safe_name = fname.replace(" ", "_").replace(":", "-")
    print(f"⬆ Uploading: {safe_name}")
    try:
    with open(local_path, 'rb') as f:
    ftp.storbinary(f"STOR {safe_name}", f, blocksize=128*1024)
    ftp.voidcmd("NOOP")
    print(f" ✅ Uploaded: {safe_name}")
    except Exception as e:
    print(f" ❌ Failed to upload {safe_name}: {e}")
    ftp.quit()
    print(" All done.")


    The problem is download works, but I am failing to upload files. I have tried whatever I could, but only the first file gets uploaded, others failed.

    Connecting to remote download folder: /users//Outbound
    Found 10 files to download...

    ⬇ Downloading: 07-25-2025_XXXXXXXXXXXX.txt
    ⬇ Downloading: 07-25-2025_XXXXXXXXXXXX.txt
    ⬇ Downloading: 07-25-2025_XXXXXXXXXXXX.txt
    ⬇ Downloading: 07-25-2025_XXXXXXXXXXXX.txt
    ✅ All downloads complete.

    Switching to upload directory: /users/Outbound
    ⬆ Uploading: 07-25-2025_XXXXXXXXXXXX.txt
    ❌ Failed to 07-25-2025_XXXXXXXXXXXX.txt: [SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:2685)
    ⬆ Uploading: 07-25-2025_XXXXXXXXXXXX.txt
    ❌ Failed to upload 07-25-2025_XXXXXXXXXXXX.txt: 426 Transfer failed
    ⬆ Uploading: 07-25-2025_XXXXXXXXXXXX.txt
    ❌ Failed to upload 07-25-2025_XXXXXXXXXXXX.txt: 200 Transfer mode set to BINARY
    ⬆ Uploading: 07-25-2025_XXXXXXXXXXXX.txt
    ❌ Failed to upload 07-25-2025_XXXXXXXXXXXX.txt: 200 Transfer mode set to BINARY
    ⬆ Uploading: 07-25-2025_XXXXXXXXXXXX.txt
    ❌ Failed to upload 07-25-2025_XXXXXXXXXXXX.txt: 200 Transfer mode set to BINARY


    All done.

    What should I do? The uploads are only working in Filezilla and in .NET with FluentFtp. In Filezilla i get this status: Status: Server sent passive reply with unroutable address. Using server address instead.

    Continue reading...

Compartilhe esta Página