FTP status problems. (Again)

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

    #1

    FTP status problems. (Again)

    Hello, I have posted before about trying to find the status of an FTP
    uplaod but couldn't get anything to work. After some more searching I
    found

    but it does not seem to work because it just uploads the file and does
    not print a . onto the screen. HEre is the code I have when I'm using
    the code from that link.
    import ftplib
    import os
    class dot_FTP(ftplib. FTP):
    def storbinary(self , cmd, fp, blocksize=8192) :
    self.voidcmd('T YPE I')
    conn = self.transfercm d(cmd)
    while 1:
    buf = fp.read(blocksi ze)
    if not buf: break
    conn.send(buf)
    sys.stdout.writ e('.')
    sys.stdout.flus h()
    conn.close()
    return self.voidresp()


    ftp = ftplib.FTP("FTP ADDRESS")
    ftp.login("user ","pass")
    file = "/file"
    ftp.storbinary( "STOR " + file, open(file, "rb"), 1024)
    ftp.quit()
    Does anyone know why this is not working? IS there any other way to
    find out when a chunc has been sent or the bytes uploaded of a file?
    Thanks.

  • marduk

    #2
    Re: FTP status problems. (Again)

    On Fri, 2005-09-16 at 19:27 -0700, Nainto wrote:[color=blue]
    > Hello, I have posted before about trying to find the status of an FTP
    > uplaod but couldn't get anything to work. After some more searching I
    > found
    > http://groups.google.com/group/comp....917c906cdc04d4
    > but it does not seem to work because it just uploads the file and does
    > not print a . onto the screen. HEre is the code I have when I'm using
    > the code from that link.
    > import ftplib
    > import os
    > class dot_FTP(ftplib. FTP):
    > def storbinary(self , cmd, fp, blocksize=8192) :
    > self.voidcmd('T YPE I')
    > conn = self.transfercm d(cmd)
    > while 1:
    > buf = fp.read(blocksi ze)
    > if not buf: break
    > conn.send(buf)
    > sys.stdout.writ e('.')
    > sys.stdout.flus h()
    > conn.close()
    > return self.voidresp()
    >
    >
    > ftp = ftplib.FTP("FTP ADDRESS")
    > ftp.login("user ","pass")
    > file = "/file"
    > ftp.storbinary( "STOR " + file, open(file, "rb"), 1024)
    > ftp.quit()
    > Does anyone know why this is not working? IS there any other way to
    > find out when a chunc has been sent or the bytes uploaded of a file?
    > Thanks.
    >[/color]

    .... and I haven't tried this myself, but you should be able to subclass
    the builtin file object and prepare your own read() method. Something
    like

    class ProgressFile(fi le):

    def read(self, size = None):
    print '.',

    if size is not None:
    return file.read(self, size)
    else:
    return file.read()

    May need some tweaking.. then store the file as

    ftp.storbinary( "STOR " + file, ProgressFile(fi le, "rb"), 1024)

    Give it a try..

    -m

    Comment

    • marduk

      #3
      Re: FTP status problems. (Again)

      On Sat, 2005-09-17 at 04:42 +0000, marduk wrote:[color=blue]
      >
      > ... and I haven't tried this myself, but you should be able to subclass
      > the builtin file object and prepare your own read() method. Something
      > like
      >
      > class ProgressFile(fi le):
      >
      > def read(self, size = None):
      > print '.',
      >
      > if size is not None:
      > return file.read(self, size)
      > else:
      > return file.read()
      >
      > May need some tweaking.. then store the file as
      >
      > ftp.storbinary( "STOR " + file, ProgressFile(fi le, "rb"), 1024)
      >
      > Give it a try..
      >
      > -m
      >[/color]

      I corrected some errors and made some modifications to my previous post:

      class ProgressFile(fi le):
      def read(self, size = None):
      from sys import stdout

      if size is not None:
      buff = file.read(self, size)
      if buff:
      stdout.write('. ')
      else:
      stdout.write('\ n')
      return buff
      else:
      buff = ''
      while True:
      new_str = file.read(self, 1024)
      stdout.write('. ')
      if new_str:
      buff = buff + new_str
      else:
      stdout.write('\ n')
      break
      return buff


      if __name__ == '__main__':
      import sys

      fname = sys.argv[1]
      f = ProgressFile(fn ame)
      f.read()


      Comment

      • Nainto

        #4
        Re: FTP status problems. (Again)

        Thanks, I'll give this a try. This will print a period right?

        Comment

        • Nainto

          #5
          Re: FTP status problems. (Again)

          When I execute the folllowing code with all of the address, username,
          and passwords filled out I gt an error saying:
          "/Library/Frameworks/Python.framewor k/Versions/2.4/Resources/Python.app/Contents/MacOS/Python"
          "/Users/zacim/Documents/FireUpFTP/foramttedthing" ; exit
          Traceback (most recent call last):
          File "/Users/zacim/Documents/FireUpFTP/foramttedthing" , line 26, in ?
          fname = sys.argv[1]
          IndexError: list index out of range
          logout
          [Process completed]

          This is the code:

          import ftplib
          class ProgressFile(fi le):
          def read(self, size = None):
          from sys import stdout

          if size is not None:
          buff = file.read(self, size)
          if buff:
          stdout.write('. ')
          else:
          stdout.write('\ n')
          return buff
          else:
          buff = ''
          while True:
          new_str = file.read(self, 1024)
          stdout.write('. ')
          if new_str:
          buff = buff + new_str
          else:
          stdout.write('\ n')
          break
          return buff
          if __name__ == '__main__':
          import sys
          fname = sys.argv[1]
          f = ProgressFile(fn ame)
          f.read()

          ftp = ftplib.FTP("ftp .sadpanda.cjb.c c")
          ftp.login("sadp anda","s4dp4nd4 b1g")
          file = "/FrenchBrochure. pages.zip.gz"
          ftp.storbinary( "STOR " + file, ProgressFile(fi le, "rb"), 1024)
          ftp.quit()

          Comment

          • Kartic

            #6
            Re: FTP status problems. (Again)

            That is because you have just taken marduk's program and included your
            ftp code... please see what he has done in the if __name__ == '__main__'
            part.

            He expects the file name as an commandline argument (sys.argv[1]) and
            since you just copied his code, you are invoking the script *without*
            the argument. This is what the exception means when it says "list index
            out of range".

            To make ProgressFile class work for you with the filename you have
            hardcoded... make the following modification to the "if __name ==" part:

            if __name__ == '__main__':
            ftp = ftplib.FTP("ftp .sadpanda.cjb.c c")
            ftp.login("sadp anda","s4dp4nd4 b1g")
            file = "/FrenchBrochure. pages.zip.gz"
            ftp.storbinary( "STOR " + file, ProgressFile(fi le, "rb"), 1024)
            ftp.quit()

            Thanks,
            -Kartic

            The Great 'Nainto' uttered these words on 9/17/2005 9:08 AM:[color=blue]
            > When I execute the folllowing code with all of the address, username,
            > and passwords filled out I gt an error saying:
            > "/Library/Frameworks/Python.framewor k/Versions/2.4/Resources/Python.app/Contents/MacOS/Python"
            > "/Users/zacim/Documents/FireUpFTP/foramttedthing" ; exit
            > Traceback (most recent call last):
            > File "/Users/zacim/Documents/FireUpFTP/foramttedthing" , line 26, in ?
            > fname = sys.argv[1]
            > IndexError: list index out of range
            > logout
            > [Process completed]
            >
            > This is the code:
            >
            > import ftplib
            > class ProgressFile(fi le):
            > def read(self, size = None):
            > from sys import stdout
            >
            > if size is not None:
            > buff = file.read(self, size)
            > if buff:
            > stdout.write('. ')
            > else:
            > stdout.write('\ n')
            > return buff
            > else:
            > buff = ''
            > while True:
            > new_str = file.read(self, 1024)
            > stdout.write('. ')
            > if new_str:
            > buff = buff + new_str
            > else:
            > stdout.write('\ n')
            > break
            > return buff
            > if __name__ == '__main__':
            > import sys
            > fname = sys.argv[1]
            > f = ProgressFile(fn ame)
            > f.read()
            >
            > ftp = ftplib.FTP("ftp .sadpanda.cjb.c c")
            > ftp.login("sadp anda","s4dp4nd4 b1g")
            > file = "/FrenchBrochure. pages.zip.gz"
            > ftp.storbinary( "STOR " + file, ProgressFile(fi le, "rb"), 1024)
            > ftp.quit()
            >[/color]

            Comment

            • Nainto

              #7
              Re: FTP status problems. (Again)

              I'm really sorry that I keep having problems with this. :-( Now I get:
              TypeError: Error when calling the metaclass bases[] str() takes at most
              1 arguement (3 given)

              and the Traceback is:

              file "formattedthing ", line 2, in '?'
              classProgressFi le(file)

              With the following code:

              import ftplib
              class ProgressFile(fi le):
              def read(self, size = None):
              from sys import stdout
              if size is not None:
              buff = file.read(self, size)
              if buff:
              stdout.write('. ')
              else:
              stdout.write('\ n')
              return buff
              else:
              buff = ''
              while True:
              new_str = file.read(self, 1024)
              stdout.write('. ')
              if new_str:
              buff = buff + new_str
              else:
              stdout.write('\ n')
              break
              return buff
              if __name__ == '__main__':
              ftp = ftplib.FTP("ftp .sadpanda.cjb.c c")
              ftp.login("sadp anda","PASSWORD EDITEDOUT")
              file = "/FrenchBrochure. pages.zip"
              ftp.storbinary( "STOR " + file, ProgressFile(fi le, "rb"), 1024)
              ftp.quit()

              Thanks so muchy for help guys. :-)

              Comment

              • Kartic

                #8
                Re: FTP status problems. (Again)

                Hello,
                [color=blue]
                > file = "/FrenchBrochure. pages.zip"
                > ftp.storbinary( "STOR " + file, ProgressFile(fi le, "rb"), 1024)[/color]

                You are using file = "/FrenchBrochure. pages.zip" (sorry I did not notice
                earlier).

                You can not use file as variable name like you have, as it represents a
                file object in python.

                Please change your variable name from file to filename.

                Thanks.

                Comment

                • Nainto

                  #9
                  Re: FTP status problems. (Again)

                  Unfortunatly I stiill get the same error. :-(
                  Here is the code:
                  import ftplib
                  class ProgressFile(fi le):
                  def read(self, size = None):
                  from sys import stdout
                  if size is not None:
                  buff = file.read(self, size)
                  if buff:
                  stdout.write('. ')
                  else:
                  stdout.write('\ n')
                  return buff
                  else:
                  buff = ''
                  while True:
                  new_str = file.read(self, 1024)
                  stdout.write('. ')
                  if new_str:
                  buff = buff + new_str
                  else:
                  stdout.write('\ n')
                  break
                  return buff
                  if __name__ == '__main__':
                  ftp = ftplib.FTP("ftp .sadpanda.cjb.c c")
                  ftp.login("sadp anda","PASSWORD EDITIEDOUT")
                  filename = "/FrenchBrochure. pages.zip"
                  ftp.storbinary( "STOR " + filename, ProgressFile(fi lename, "rb"), 1024)

                  ftp.quit()


                  Thanks again.

                  Comment

                  • Kartic

                    #10
                    Re: FTP status problems. (Again)

                    > Unfortunatly I stiill get the same error. :-(

                    The same "metaclass bases[]" error??
                    [color=blue]
                    > Here is the code:
                    > import ftplib[/color]
                    -snip-[color=blue]
                    > if __name__ == '__main__':
                    > ftp = ftplib.FTP("ftp .sadpanda.cjb.c c")
                    > ftp.login("sadp anda","PASSWORD EDITIEDOUT")
                    > filename = "/FrenchBrochure. pages.zip"
                    > ftp.storbinary( "STOR " + filename, ProgressFile(fi lename, "rb"), 1024)
                    >
                    > ftp.quit()[/color]


                    Your code works for me.. I just made one minor change.

                    I gave a different target name:

                    filename = "/FrenchBrochure. pages.zip"
                    target = "FrenchBrochure .pages.zip"
                    ftp.storbinary( "STOR " + target, ProgressFile(fi lename, "rb"), 1024)

                    Thanks,
                    -Kartic

                    Comment

                    • Nainto

                      #11
                      Re: FTP status problems. (Again)

                      It works! Thanks so much for your help!

                      Comment

                      Working...