anon_user123456
anon_user123456

Reputation: 69

Python FTP: "ftplib.error_temp: 421 Timeout - try typing a little faster next time" error

I have a program which interacts with a FTP server whenever the user issues a command. Here is the basic structure of my code:

from ftplib import FTP

ftp = FTP(host=host)
login_status = ftp.login(user=username, passwd=password)

while True:
    command = input()
    if command == "abc":
        ftp.storbinary(textfile, textmessage1)
    elif command == "def":
        ftp.storbinary(textfile, textmessage2)

Problem is, if I wait for about 20 seconds between issuing commands (i.e if I leave the program for about 20 seconds), and tries to issue a command after the 20s gap, then this error message pops up: ftplib.error_temp: 421 Timeout - try typing a little faster next time

It is in my understanding that ftp servers have a time limit and will kick you after inactivity. I'm looking for a way to keep the FTP server busy and stop letting it kick my program off. Basically, any solution that prevents that error message from showing again.

Thanks in advance!

Upvotes: 1

Views: 2082

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202232

You will have to either:

  • Login only after prompting the user.
  • or keep the connection alive while prompting the user by executing some dummy commands. Typically that is done by sending NOOP command:

    ftp.voidcmd("NOOP")
    

    Though some servers ignore the NOOP command. Then you will have to send some commands that really do something, like PWD.

    With blocking input call, you will have to send the command on another thread, see
    Do something while user input not received

Upvotes: 1

Related Questions