ftplib callbacks

M

Matija Papec

I would like to reimplement ftplib "nlst" using ftplib.dir (ftp server
doesn't support nlst) so I'm trying to guess how to use ftp callbacks.
Any help is appreciated.
tia!

============
#!/usr/bin/python

# nlst vs list

import pprint
pp = pprint.PrettyPrinter(indent=4)

import ftplib

def myfilter(line):
return line

ftp = ftplib.FTP("localhost")
ftp.login("mpapec", "island88")
#print ftp.retrlines("LIST")
dir = ftp.dir("", myfilter)
print dir
 
S

Steve Holden

Matija said:
I would like to reimplement ftplib "nlst" using ftplib.dir (ftp server
doesn't support nlst) so I'm trying to guess how to use ftp callbacks.
Any help is appreciated.
tia!

============
#!/usr/bin/python

# nlst vs list

import pprint
pp = pprint.PrettyPrinter(indent=4)

import ftplib

def myfilter(line):
return line

ftp = ftplib.FTP("localhost")
ftp.login("mpapec", "island88")
#print ftp.retrlines("LIST")
dir = ftp.dir("", myfilter)
print dir
The callback is called each time the FTP client softwrae has something
to deal with. Try (untested):

lines = []

def myfilter(line):
global lines
lines.append(line)

with the remainder of your code unchanged. The lines list will then
contain a list of all lines.

The problem with your current callback is that its return value is
ignored by the FTP module that calls it.

regards
Steve
 
M

Matija Papec

X-Ftn-To: Steve Holden

Steve Holden said:
The callback is called each time the FTP client softwrae has something
to deal with. Try (untested):

lines = []

def myfilter(line):
global lines
lines.append(line)

with the remainder of your code unchanged. The lines list will then
contain a list of all lines.

Tnx, I did

def nlst2(self, *args):
cmd = 'LIST'
for arg in args:
cmd = cmd + (' ' + arg)
files = []
self.retrlines(cmd, files.append)
return files

and now I just need to parse it. :)
The problem with your current callback is that its return value is
ignored by the FTP module that calls it.

Yes, unfortunately even "def retrlines" returns something uninteresting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,779
Messages
2,569,606
Members
45,239
Latest member
Alex Young

Latest Threads

Top