[ftputil] Empty listdir in non empty directory

Stefan Schwarzer sschwarzer at sschwarzer.net
Thu May 4 22:52:10 CEST 2017


Matthias, thanks for reporting. Rodrigo, thanks for
helping Matthias. :-)

On 2017-05-04 14:26, Rodrigo Santellan wrote:
> For what I see there is no -a flag on the ftputils library.
> 
> I'm thinking that the problem could be the ftplib library (
> https://docs.python.org/3/library/ftplib.html).
> 
> I can't find any documentation about why is passing the -a flag. What it
> does on the ftp protocol. What operating system are you running the script?

The problem here is that ftputil sends a `-a` flag to tell
the server to list "all" files (=including hidden files,
whose names start with a dot `.`). This seems to work for
many servers, but not for all. Some servers see `-a` as the
name of a directory or file to list, and if there's no such
directory or file, nothing is listed.

You can avoid that ftputil passes `-a` by setting

  ftp_host.use_list_a_option = False

after creating the `FTPHost` instance. See

  http://ftputil.sschwarzer.net/trac/wiki/Documentation#id12

I'm sorry for this side effect. Maybe in hindsight I
wouldn't have used `-a` by default, but now I don't want to
change this to avoid breaking backward compatibility.

TODO for me: Add this situation to the FAQ section of the
ftputil documentation.

Best regards,
Stefan


> On Thu, May 4, 2017 at 5:10 AM, Mattias <mb at bokesand.se> wrote:
> 
>> Thanks for your suggestion.
>> I made the testing program listed below and got the result also listed
>> below.
>> It looks like the WinCE ftp-server doesn't respond to LIST -a.
>> I made some testing in ordinary commandline ftp-client and I get correct
>> result from "dir" and "ls", but empty result from "dir -a" and "ls -a".
>> (verified with wireshark that ftp-client issue "LIST -a" when I type "dir
>> -a")
>> Is it possible to remove the -a when using ftputil?
>>
>> Best Regards
>> Mattias
>>
>>
>>
>> import ftplib
>> import ftputil
>> import ftputil.session
>>
>> my_session_factory = ftputil.session.session_factory(
>> base_class=ftplib.FTP,
>> port=21,
>> encrypt_data_channel=False,
>> debug_level=2)
>>
>> host = ftputil.FTPHost('192.168.10.83', 'ABC', '123',
>> session_factory=my_session_factory)
>> names = host.listdir(host.curdir)
>> print (names)
>> host.close()
>>
>>
>>
>> python MoveFinishedLogs3.py
>> *cmd* 'USER ABC'
>> *put* 'USER ABC\r\n'
>> *get* '331 User name okay, need password.\r\n'
>> *resp* '331 User name okay, need password.'
>> *cmd* 'PASS ***'
>> *put* 'PASS ***\r\n'
>> *get* '230 User logged in, proceed.\r\n'
>> *resp* '230 User logged in, proceed.'
>> *cmd* 'PWD'
>> *put* 'PWD\r\n'
>> *get* '257 "/".\r\n'
>> *resp* '257 "/".'
>> *cmd* 'CWD /'
>> *put* 'CWD /\r\n'
>> *get* '250 Requested file action okay, completed.\r\n'
>> *resp* '250 Requested file action okay, completed.'
>> *cmd* 'CWD /'
>> *put* 'CWD /\r\n'
>> *get* '250 Requested file action okay, completed.\r\n'
>> *resp* '250 Requested file action okay, completed.'
>> *cmd* 'TYPE A'
>> *put* 'TYPE A\r\n'
>> *get* '200 Command okay.\r\n'
>> *resp* '200 Command okay.'
>> *cmd* 'PASV'
>> *put* 'PASV\r\n'
>> *get* '227 Entering Passive Mode (192,168,10,83,192,35).\r\n'
>> *resp* '227 Entering Passive Mode (192,168,10,83,192,35).'
>> *cmd* 'LIST -a'
>> *put* 'LIST -a\r\n'
>> *get* '125 Data connection already open; transfer starting.\r\n'
>> *resp* '125 Data connection already open; transfer starting.'
>> *get* '226 Closing data connection. \r\n'
>> *resp* '226 Closing data connection. '
>> *cmd* 'CWD /'
>> *put* 'CWD /\r\n'
>> *get* '250 Requested file action okay, completed.\r\n'
>> *resp* '250 Requested file action okay, completed.'
>> []
>>
>> On 4 May 2017 at 04:09:33 +02:00, Rodrigo Santellan <rsantellan at gmail.com>
>> wrote:
>>
>> Hi Mattias,
>> When I had troubles with the connection I increased the debug level.
>>
>> Can you try: http://ftputil.sschwarzer.net/trac/wiki/Documentation#
>> session-factories creating a session factory with debug 2 and posting the
>> output?
>> Regards
>>
>> On Wed, May 3, 2017 at 5:32 PM, Mattias <mb at bokesand.se> wrote:
>>
>> Hi
>> I have a problem with ftputil.
>> I get empty result fromlistdir.
>> If it matters my ftp-server runs on Windows CE 7.0 and the client running
>> my python script runs on a raspberry pi (raspbian jessie lite)
>>
>>
>> host = ftputil.FTPHost('192.168.10.83', 'abc', '123')
>> names = host.listdir(host.curdir)
>> print (names)
>>
>> returns just []
>>
>> But:
>> # File: ftplib-example-1.py
>> import ftplib
>> ftp = ftplib.FTP("192.168.10.83")
>> ftp.login("abc", "123")
>> data = []
>> ftp.dir(data.append)
>> ftp.quit()
>> for line in data:
>>     print "-", line
>>
>> returns a long list like
>> ...
>> - 06-08-15  19:57                64505 Elekta20150608_13_48.csv
>> - 06-11-15  19:31                87932 Elekta20150611_11_14.csv
>> - 06-16-15  18:51                78498 Elekta20150616_11_37.csv
>> ...
>>
>>
>> Any ideas what could be wrong?
>>
>> Thanks in advance
>> /Mattias Bokesand
>>
>> _______________________________________________
>> ftputil mailing list
>> ftputil at lists.sschwarzer.net
>> http://lists.sschwarzer.net/listinfo/ftputil
>>
>>
>>
> 
> 
> 
> _______________________________________________
> ftputil mailing list
> ftputil at lists.sschwarzer.net
> http://lists.sschwarzer.net/listinfo/ftputil
> 



More information about the ftputil mailing list