Is it possible to hide some files ?

88 views
Skip to first unread message

Yannis B

unread,
Sep 28, 2011, 5:47:10 PM9/28/11
to Python FTP server library - Discussion group
Hello,

In OS X, I would like to hide the "Icon" file names when I'm browsing
the shared directory and the subs.

Congratulations for the 0.6 version.

Bye

Giampaolo Rodolà

unread,
Sep 29, 2011, 3:34:56 AM9/29/11
to pyft...@googlegroups.com
Hi,
you can override AbstractedFS.listdir method. 
In there, you can return a filtered list of file names.
...something like:


from pyftpdlib import ftpserver

class CustomizedFS(ftpserver.AbstractedFS):

    def listdir(self, path):
        ls = os.listdir(path)
        return [x for x in ls if not x.endswith('.ico')]

authorizer = ftpserver.DummyAuthorizer()
authorizer.add_anonymous(os.getcwd())
handler = ftpserver.FTPHandler
handler.authorizer = authorizer
handler.abstracted_fs = CustomizedFS
server = ftpserver.FTPServer(('', 21), handler)
server.serve_forever()


Hope this helps,

Giampaolo

Yannis B

unread,
Oct 1, 2011, 6:51:07 PM10/1/11
to Python FTP server library - Discussion group
Thanks, it's working great.

Do you confirm that a "hidden" files cannot be downloaded by the
client even if he knows that it really exists and does a RETR command?

Giampaolo Rodolà

unread,
Oct 1, 2011, 7:35:34 PM10/1/11
to pyft...@googlegroups.com
No, i do not confirm. =)
Of course it can. 
listdir() method dictates what the FTP server returns on LIST, while RETR is governed by AbstractedFS.open() method.
In order to filter client downloads you should override open() method as well, keeping in mind that in case of RETR, open is called with mode="r" argument.
So, let's say you want to deny the download of files with .ico extension, you would end up doing something like this:


class CustomizedFS(ftpserver.AbstractedFS):

    def listdir(self, path):
        ls = os.listdir(path)
        return [x for x in ls if not x.endswith('.ico')]

     def open(self, path, mode):
          if "r" in mode and path.endswith(.'ico'):
              # ENOENT is "no such file or directory";
              raise OSError(errno.ENOENT)
          return open(path, mode)

Hope this helps,

G.

Yannis B

unread,
Oct 2, 2011, 2:38:09 PM10/2/11
to Python FTP server library - Discussion group
OK, I tested the code, but I realized that the

raise OSError(errno.ENOENT)

gave me a traceback. Not nice.
Instead, I put

return open('//', 'r')


but it does not give the right message in the 550 code.
I then tried

return open('//somethingelse', 'r')

giving me the right message, but that's not nice, if the file
'seomethingelse' exists !

I'm sure there is a right way to do that, but I'm not a programmer at
all


Giampaolo Rodolà

unread,
Oct 2, 2011, 2:58:01 PM10/2/11
to pyft...@googlegroups.com


2011/10/2 Yannis B <lapostem...@gmail.com>

OK, I tested the code, but I realized that the

                raise OSError(errno.ENOENT)

gave me a traceback. Not nice.

What traceback?
Maybe you didn't import errno module.
Add:

import errno

...at the top of the file.

G.

Yannis B

unread,
Oct 2, 2011, 3:27:23 PM10/2/11
to Python FTP server library - Discussion group
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/asyncore.py", line 83, in read
obj.handle_read_event()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/asyncore.py", line 442, in handle_read_event
self.handle_read()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/asynchat.py", line 158, in handle_read
self.found_terminator()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/site-packages/pyftpdlib/ftpserver.py", line 1996, in
found_terminator
self.process_command(cmd, arg)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/site-packages/pyftpdlib/ftpserver.py", line 2005, in
process_command
method(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/site-packages/pyftpdlib/ftpserver.py", line 2643, in
ftp_RETR
fd = self.run_as_current_user(self.fs.open, file, 'rb')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/site-packages/pyftpdlib/ftpserver.py", line 2253, in
run_as_current_user
return function(*args, **kwargs)
File "../ftptest.py", line 14, in open
raise OSError(errno.ENOENT)
OSError: 2



obtained with the ftptest.py file :

import os
import errno
from pyftpdlib import ftpserver

class CustomizedFS(ftpserver.AbstractedFS):

def listdir(self, path):
ls = os.listdir(path)
return [x for x in ls if not x.endswith('.zip')]


def open(self, path, mode):
if "r" in mode and path.endswith('.zip'):
raise OSError(errno.ENOENT)
return open(path, mode)

authorizer = ftpserver.DummyAuthorizer()
authorizer.add_anonymous(os.getcwd())
handler = ftpserver.FTPHandler
handler.authorizer = authorizer
handler.abstracted_fs = CustomizedFS
server = ftpserver.FTPServer(('192.168.1.2', 2121), handler)
server.serve_forever()

Giampaolo Rodolà

unread,
Oct 2, 2011, 3:32:33 PM10/2/11
to pyft...@googlegroups.com
Oh, right, sorry.
Do:

raise IOError(errno.ENOENT)

...instead of:

 raise OSError(errno.ENOENT)

G.

2011/10/2 Yannis B <lapostem...@gmail.com>

--
You received this message because you are subscribed to the "Python FTP server library" project group:
http://code.google.com/p/pyftpdlib/
To post to this group, send email to pyft...@googlegroups.com
To unsubscribe from this group, send email to pyftpdlib-...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/pyftpdlib

Yannis B

unread,
Oct 2, 2011, 3:36:52 PM10/2/11
to Python FTP server library - Discussion group
mmmm

getting another traceback

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/asyncore.py", line 83, in read
obj.handle_read_event()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/asyncore.py", line 442, in handle_read_event
self.handle_read()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/asynchat.py", line 158, in handle_read
self.found_terminator()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/site-packages/pyftpdlib/ftpserver.py", line 1996, in
found_terminator
self.process_command(cmd, arg)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/site-packages/pyftpdlib/ftpserver.py", line 2005, in
process_command
method(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/site-packages/pyftpdlib/ftpserver.py", line 2645, in
ftp_RETR
why = _strerror(err)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7/site-packages/pyftpdlib/ftpserver.py", line 212, in
_strerror
return os.strerror(err.errno)
TypeError: an integer is required
Reply all
Reply to author
Forward
0 new messages