[ftputil] "Conditional" FTP Login
Stefan Schwarzer
sschwarzer at sschwarzer.net
Sun Nov 18 10:50:11 CET 2007
Hello Franz,
On 2007-11-17 18:36, Franz Buchinger wrote:
> I'm currently developing a script that connects to ftp servers using
> ftputil. i want to implement a "conditional" ftp login: if the ftp
> server is anonymous, the script should connect automatically. if the
> server requires a login, the user should be prompted to raw_input his
> credentials.
>
> I suppose I have to create a SessionFactory for this... can someone give
> further hints?
depending on your needs, you may not need a SessionFactory.
Possibly approach (untested):
import getpass
import ftputil
from ftputil import ftp_error
try:
# try anonymous access
ftp_host = ftputil.FTPHost(server, "anonymous", email_address)
except ftp_error.PermanentError:
# login failed, so try with userid and password
userid = raw_input("Userid: ")
password = getpass.getpass("Password: ")
ftp_host = ftputil.FTPHost(server, userid, password)
On the other hand, there may be other reasons for the first
login attempt to fail with a PermanentError. So you would have
to catch with "except ftp_error.PermanentError, exception" and
examine (e. g. parse) the exception object.
Depending on the server, though, there could be other exception
classes. For "my" server software, PureFTPd, I get a general
FTPOSError if I try an anonymous login. What are your results
for the servers you tried?
In summary, there doesn't seem to be an approach that is easy
and robust at the same time. You should experiment with variants
of the code above.
Best regards,
Stefan
More information about the ftputil
mailing list