File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -345,13 +345,18 @@ def progress_callback(data):
345345 print (f"\n Uploaded { file } to { selected_ip } ." )
346346 except ftplib .all_errors as e :
347347 print (f"FTP error: { e } " )
348+ ftp = None # Mark ftp as unusable
348349
350+ # Only attempt to send BYE if ftp is connected and has a socket
349351 try :
350- ftp .sendcmd ("BYE" )
351- print (f"Disconnected from { selected_ip } ." )
352- except ftplib .all_errors as e :
353- if str (e ).strip ().lower () == "timed out" :
354- # Suppress expected timeout after BYE
352+ if ftp is not None and getattr (ftp , 'sock' , None ) is not None :
353+ ftp .sendcmd ("BYE" )
355354 print (f"Disconnected from { selected_ip } ." )
355+ else :
356+ print (f"No active FTP connection to disconnect from." )
357+ except (* ftplib .all_errors , AttributeError ) as e :
358+ # Handle timeout or already closed connection
359+ if isinstance (e , TimeoutError ) or (str (e ).strip ().lower () == "timed out" ):
360+ print (f"Disconnected from { selected_ip } (timeout after BYE, device likely restarted)." )
356361 else :
357362 print (f"FTP error after BYE: { e } " )
You can’t perform that action at this time.
0 commit comments