1

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!

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Leo Feng
  • 59
  • 8
  • and if you try put a time out like this : ftp = FTP(host=host, timeout=100) – GiovaniSalazar Nov 17 '19 at 01:29
  • @GiovaniSalazar So if I put a really large number for the timeout parameter, the FTP server will have a larger timeout allowance? Also, what is the unit for the timout variable, seconds or milliseconds? – Leo Feng Nov 17 '19 at 01:31
  • Of course not, `timeout` parameter is for local use only. If has no effect on the server. – Martin Prikryl Nov 17 '19 at 07:31

1 Answers1

1

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

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Thanks for the comment. One question, what does the PWD command actually do? Really, since I'm only interacting with text files in the server, any commands that doesn t affect the files is okay. – Leo Feng Nov 17 '19 at 07:35
  • `PWD` command returns the current remote working directory. So it has no side effect on files or anything else. In ftplib, you can send it by calling `FTP.pwd` method. – Martin Prikryl Nov 17 '19 at 10:47