ftplib callbacks

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matija Papec

    #1

    ftplib callbacks


    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.PrettyPr inter(indent=4)

    import ftplib

    def myfilter(line):
    return line

    ftp = ftplib.FTP("loc alhost")
    ftp.login("mpap ec", "island88")
    #print ftp.retrlines(" LIST")
    dir = ftp.dir("", myfilter)
    print dir


    --
    Matija
  • Steve Holden

    #2
    Re: ftplib callbacks

    Matija Papec wrote:
    [color=blue]
    > 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.PrettyPr inter(indent=4)
    >
    > import ftplib
    >
    > def myfilter(line):
    > return line
    >
    > ftp = ftplib.FTP("loc alhost")
    > ftp.login("mpap ec", "island88")
    > #print ftp.retrlines(" LIST")
    > dir = ftp.dir("", myfilter)
    > print dir
    >
    >[/color]
    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(li ne)

    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
    --


    Holden Web LLC +1 800 494 3119

    Comment

    • Matija Papec

      #3
      Re: ftplib callbacks

      X-Ftn-To: Steve Holden

      Steve Holden <steve@holdenwe b.com> wrote:[color=blue]
      >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(li ne)
      >
      >with the remainder of your code unchanged. The lines list will then
      >contain a list of all lines.[/color]

      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. :)
      [color=blue]
      >The problem with your current callback is that its return value is
      >ignored by the FTP module that calls it.[/color]

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



      --
      Matija

      Comment

      Working...