Using Python 3.11.3 on MacOS Ventura 13.3.1 I've been troubleshooting an error I receive after successfully uploading one file using ftplib and ssl. I can successfully connect to the FTPS server but upon uploading one file the FTPS connection abruptly closes and I get this error in the Traceback:
Traceback (most recent call last):
File "/Users/myuser/MyBin/TTB/Python/ftplib_test2.py", line 67, in <module>
ftps.storbinary(f"STOR {remote_file}", file)
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ftplib.py", line 508, in storbinary
conn.unwrap()
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ssl.py", line 1322, in unwrap
s = self._sslobj.shutdown()
^^^^^^^^^^^^^^^^^^^^^^^
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:2702)
FTP closed
Here's my Python code:
import ftplib
import ssl
import traceback
from datetime import datetime, timedelta
import socket
from pathlib import Path
# Set myPath and myPathLFTP to ~/Documents
myPath = Path.home() / 'Documents'
myPathLFTP = Path.home() / 'Documents'
date_input = "2023-05-01"
dt = datetime.strptime(date_input, '%Y-%m-%d')
f_mon = (dt + timedelta(days=0)).strftime('%Y%m%d')
f_tues = (dt + timedelta(days=1)).strftime('%Y%m%d')
dir_week = (datetime.strptime(date_input, '%Y-%m-%d') + timedelta(days=0)).strftime('%Y-%m-%d')
ftp_host = 'ftp.myftp.com'
ftp_user = 'myuser'
ftp_pass = 'mypassword'
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
# Disable certificate verification
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
#context.options = ssl.OP_NO_RENEGOTIATION
#context.options = ssl.OP_NO_COMPRESSION
# Create an FTP_TLS instance
ftps = ftplib.FTP_TLS(ftp_host, context=context, encoding='utf-8')
# Set reuse_address to True
ftps.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
# Connect to the FTP server and login
ftps.connect(ftp_host, 21)
ftps.login(user=ftp_user, passwd=ftp_pass)
ftps.prot_p() # switch to secure data connection
# Welcome message
welcome = ftps.getwelcome()
print(welcome)
ftps.retrlines('LIST') # list directory content securely
# Specify the local file path and the destination file name on the server
local_files = [
f'{myPath}/{dir_week}/{f_mon}_audio.mp3',
f'{myPath}/{dir_week}/{f_tues}_audio.mp3'
]
remote_files = [
f'{f_mon}_audio.mp3',
f'{f_tues}_audio.mp3'
]
for local_file, remote_file in zip(local_files, remote_files):
with open(local_file, 'rb') as file:
ftps.storbinary(f"STOR {remote_file}", file)
except Exception as e:
traceback.print_exc() # Print the traceback information
finally:
ftps.quit()
print("FTP closed")
Here's some info from the FTPS server:
openssl s_client -connect ftp.myserver.com:990 -starttls ftp
CONNECTED(00000005)
read:errno=0
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 0 bytes and written 297 bytes
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
Protocol : TLSv1.3
Cipher : 0000
Session-ID:
Session-ID-ctx:
Master-Key:
Start Time: 1685421631
Timeout : 7200 (sec)
Verify return code: 0 (ok)
I've tried uncommenting both these options to no avail...same error. So these options don't do anything for me.
#context.options = ssl.OP_NO_RENEGOTIATION
#context.options = ssl.OP_NO_COMPRESSION
I've tried uninstalling and reinstalling ftplib. I've read through the https://docs.python.org/3/library/ssl.html but I haven't found anything I'm doing wrong?
I was expecting to upload both the mp3 files in the list I provided but it stopped after one file and exited from the FTP with the error provided above.