[Tutor] FTP Errors

Peter Otten __peter__ at web.de
Wed Jul 1 18:16:06 EDT 2020


John Weller wrote:

> I have a program that runs (should run) 24/7 in a loop.  In the loop the
> program captures some data which it stores in a string.  The string is
> appended to a list and the earliest string popped to keep it at 10 lines. 
> The list is then uploaded to a web server using FTP.  It then goes to
> sleep for 5 minutes before repeating.
> 
>  
> 
> I had some problems with the program crashing when the internet dropped
> for a short time so put a try, except section in to trap the errors which
> appeared to work however I am now getting the program crashing again when
> it tries to upload instead of the except catching the problem.  It will
> work perfectly uploading every 5 minutes for hours on end then will crash.
> 
>  
> 
> The upload function is below:
> 
>  
> 
> def upload(file1, file2):
> 
>     try:
> 
>         ftp = FTP(data.ftp_host)     # connect to host, default port
> 
>         ftp.login(user=data.ftp_user, passwd=data.ftp_passwd)
> 
>     except ftplib.all_errors as err:

Study the traceback carfully: the line above is the problem. 
You probably wrote

from ftplib import FTP

or

from ftplib import *

In both cases the name ftplib is not available to your module. To fix this 
you can add the line

import ftplib

I recommend that for consistency you also change

>         ftp = FTP(data.ftp_host)     # connect to host, default port

to

          ftp = ftplib.FTP(data.ftp_host)

and remove the from ... import.

>         #Log the error
> 
>         logging.error(err)
> 
>     else:
> 
>         ftp.cwd(data.ftp_dir)
> 
>         ftp.storlines("STOR " + file1, open(file1, 'rb'))
> 
>         ftp.storlines("STOR " + file2, open(file2, 'rb'))
> 
>         ftp.quit() <ftp://ftp.quit()>
> 
>  
> 
> The error messages are below:
> 
>  
> 
> Traceback (most recent call last):
> 
>   File "garage_11.py", line 245, in upload
> 
>     ftp.login(user=data.ftp_user, passwd=data.ftp_passwd)
> 
>   File "/usr/lib/python3.7/ftplib.py", line 420, in login
> 
>     resp = self.sendcmd('PASS ' + passwd)
> 
>   File "/usr/lib/python3.7/ftplib.py", line 273, in sendcmd
> 
>     return self.getresp()
> 
>   File "/usr/lib/python3.7/ftplib.py", line 246, in getresp
> 
>     raise error_perm(resp)
> 
> ftplib.error_perm: 530 Login incorrect.
> 
>  
> 
> During handling of the above exception, another exception occurred:
> 
>  
> 
> Traceback (most recent call last):
> 
>   File "garage_11.py", line 450, in <module>
> 
>     loop(data, values_list)
> 
>   File "garage_11.py", line 372, in loop
> 
>     upload('garage.html', data.filename)
> 
>   File "garage_11.py", line 246, in upload
> 
>     except ftplib.all_errors as err:
> 
> NameError: name 'ftplib' is not defined
> 
>  
> 
> It looks as though the problem lies in the exception handling and I would
> be grateful for any pointers 😊
> 
>  
> 
> TIA
> 
>  
> 
> John
> 
>  
> 
> John Weller
> 
> 01380 723235
> 
> 07976 393631
> 
>  
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list