FTP (ftplib) output capture

Simon Forman rogue_pedro at yahoo.com
Mon Jul 31 12:49:53 EDT 2006


ChaosKCW wrote:
> Hi
>
> Has anyone caputerd the output from the std ftp lib? It seems a bit
> annoying that everything is printed to stdout. It means incorporating
> this into any real program is a problem. It would have been much better
> if they used the std logging module and hooked up a console logger for
> the feault ftp application. Alas it isnt that way.
>
> Capturing stdout like this??? :
>
> import sys
>
> sys.stdout = open('bb', 'w)
>
> But I want to re-route it to the logging module not a file , so do I
> need to write a steam object?
>
> Thanks,

ftplib pre-dates the standard logging system by a bit.

I think ftplib only prints stuff if you set its (the FTP class
instances') debug level to greater than 0.

If you really want to replace sys.stdout with something that passes the
data to a logger, then something like the following (untested) class
should do it:

class FileLog:
    def __init__(self, log):
        self.log = log
    def write(self, data):
        self.log.debug(data)

The best solution might be to subclass or rewrite FTP to really do what
you want.  I believe one of the goals of Python-3000 is better
integration of the standard library with the new-ish standard logging
system, so if you do a good job send it in.  ;-)

Peace,
~Simon




More information about the Python-list mailing list