ftplib MLSD command gives 500 Unknown command

2020-02-12 19:00发布

问题:

I have been using ls = f.mlsd() to get list of files and timestamp from ftp but it gives me

ftplib.error_perm: 500 Unknown command

Is there any problem with ftp server? do i need to install anything on the server to get this command working

回答1:

In the fact, MLSD is nothing but a protocol extension introduced in RFC 3659 that may be not supported by some FTP servers. If you care about portability, it's better to use f.nlst() instead.

If changing something on server is acceptable for you, then I suggest you switching to proftpd which has MLSD support as a part of it's mod_facts extension.



回答2:

MLSD command was not a part of an original FTP standard. It was added only later in RFC 3659, in 2007. While that's still quite some time ago, even now some major FTP servers do not support it. Particularly IIS and vsftpd.

If you need timestamps and yet you need to talk to a server that does not support MLSD command, you have two options:

  1. Use FTP.dir (LIST command). And parse proprietary format of file listing to retrieve the timestamps.

  2. Use FTP.nlst to retrieve a list of files (and folders). Then, use FTP.voidcmd to send MDTM command for each of the listed files. MDTM returns a file timestamp in a standardized format.

    Obviously this is a way less effective than the previous approach, but you won't have to deal with a proprietary formats of directory listings.

    Note that MDTM is not supported by all FTP servers either, but it is more widely supported than MLSD, although both commands come from the same RFC (3659). Particularly one of common Linux FTP servers, vsftpd, supports MDTM, but not MLSD.


For a code to implement both approaches see my answer to:
How to get FTP file's modify time using Python ftplib