I'm running into some trouble when trying to send a file using FTP. It seems like the file is sent successfully but then I get the error: FTP 451 Error closing file.--[5370]--. My setup: System running FTP server, with a PLC on the network trying to send to the FTP server on the other system.
See here the command line FTP commands and response to try to send the file from the PLC:
ftp> Send C:\blabla
200 PORT command successful.
150 Opening ASCII mode data connection. (192,168,1,100,58508)
451 Error closing file.--[5370]--
ftp: 11738 bytes sent in 0.00Seconds 5869.00Kbytes/sec.
This is the python code that i have running on the PLC:
def upload_file(ftp_server, ftp_user, ftp_password, file_path, remote_path):
try:
ftp = ftplib.FTP(ftp_server)
ftp.login(user=ftp_user, passwd=ftp_password)
with open(file_path, 'rb') as file:
ftp.cwd(remote_path)
filename = os.path.basename(file_path)
ftp.storbinary(f'STOR {filename}', file)
print(f"Successfully uploaded {filename} to {remote_path} on {ftp_server}.")
ftp.quit()
return True, ""
except ftplib.all_errors as e:
print(f"FTP error: {e}")
return False, f"FTP error: {e}"
ftp_server = ''
ftp_user = ''
ftp_password = ''
plc = pyads.Connection('127.0.0.1.1.1', pyads.PORT_TC3PLC1)
plc.open()
running = True
while(running):
Send_file = plc.read_by_name('PLC variable', pyads.PLCTYPE_BOOL)
if Send_file:
source_path = plc.read_by_name('PLC variable', pyads.PLCTYPE_STRING)
destination_path = plc.read_by_name('PLC variable', pyads.PLCTYPE_STRING)
is_successful, error_message = upload_file(ftp_server, ftp_user, ftp_password, source_path, destination_path)
plc.write_by_name('PLC variable', is_successful)
if is_successful:
print("File transfer completed successfully.")
else:
print("File transfer failed.")
plc.write_by_name('PLC variable', error_message)
plc.write_by_name('PLC variable', True)
plc.close()
This code works fine when writing to another directory on the destination system, but when i try writing to this other directory it gives me the error:
FTP 451 Error closing file.--[5370]--.
I tried looking online for this error but there is nothing about this exact error. Does someone know a possibility to fix this error? Or a direction to look in?
For anyone with the same error, I managed to figure out what the problem was. The format (content) of the file I was trying to send was not correct or not as expected by the receiving system. So, check if there are mistakes in your file, if you have another file that you 100% know is correct, compare it with the file you are trying to send.