How to write a python program to tranfer file using FTP?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Amit Vaity
    New Member
    • Jul 2010
    • 5

    How to write a python program to tranfer file using FTP?

    I have a Windows Server 2003 which is a Radius server and logs the details about the LOGINS done on a Router. I need to move the log file from the server to the client Windows XP machine using FTP but via a pyhton script.

    Any help will be appreciated as I am a newbiee to python.

    Thanks,
    Amit
  • cr4sh
    Banned
    New Member
    • Jul 2010
    • 20

    #2
    Code:
    >>> from ftplib import FTP
    >>> ftp = FTP('ftp.cwi.nl')   # connect to host, default port
    >>> ftp.login()               # user anonymous, passwd anonymous@
    >>> ftp.retrlines('LIST')     # list directory contents
    total 24418
    drwxrwsr-x   5 ftp-usr  pdmaint     1536 Mar 20 09:48 .
    dr-xr-srwt 105 ftp-usr  pdmaint     1536 Mar 21 14:32 ..
    -rw-r--r--   1 ftp-usr  pdmaint     5305 Mar 20 09:48 INDEX
     .
     .
     .
    >>> ftp.retrbinary('RETR README', open('README', 'wb').write)
    '226 Transfer complete.'
    >>> ftp.quit()
    >>> from ftplib import FTP_TLS
    >>> ftps = FTP_TLS('ftp.python.org')
    >>> ftps.login()           # login anonymously before securing control channel
    >>> ftps.prot_p()          # switch to secure data connection
    >>> ftps.retrlines('LIST') # list directory content securely
    total 9
    drwxr-xr-x   8 root     wheel        1024 Jan  3  1994 .
    drwxr-xr-x   8 root     wheel        1024 Jan  3  1994 ..
    drwxr-xr-x   2 root     wheel        1024 Jan  3  1994 bin
    drwxr-xr-x   2 root     wheel        1024 Jan  3  1994 etc
    d-wxrwxr-x   2 ftp      wheel        1024 Sep  5 13:43 incoming
    drwxr-xr-x   2 root     wheel        1024 Nov 17  1993 lib
    drwxr-xr-x   6 1094     wheel        1024 Sep 13 19:07 pub
    drwxr-xr-x   3 root     wheel        1024 Jan  3  1994 usr
    -rw-r--r--   1 root     root          312 Aug  1  1994 welcome.msg
    '226 Transfer complete.'
    >>> ftps.quit()
    >>>
    Last edited by bvdet; Jul 26 '10, 01:07 PM. Reason: Add code tags

    Comment

    • Amit Vaity
      New Member
      • Jul 2010
      • 5

      #3
      Thnx for the reply...But does this program run on Windows?

      Comment

      • cr4sh
        Banned
        New Member
        • Jul 2010
        • 20

        #4
        I don't see any reason why it shouldn't.

        Comment

        • Amit Vaity
          New Member
          • Jul 2010
          • 5

          #5
          1) Where do I put the IP address of the server from where I have to download the file?

          2) How can I authenticate a user he downloads the file?

          Thanks,
          Amit

          Comment

          • Amit Vaity
            New Member
            • Jul 2010
            • 5

            #6
            Originally posted by cr4sh
            Code:
            >>> from ftplib import FTP
            >>> ftp = FTP('ftp.cwi.nl')   # connect to host, default port
            >>> ftp.login()               # user anonymous, passwd anonymous@
            >>> ftp.retrlines('LIST')     # list directory contents
            total 24418
            drwxrwsr-x   5 ftp-usr  pdmaint     1536 Mar 20 09:48 .
            dr-xr-srwt 105 ftp-usr  pdmaint     1536 Mar 21 14:32 ..
            -rw-r--r--   1 ftp-usr  pdmaint     5305 Mar 20 09:48 INDEX
             .
             .
             .
            >>> ftp.retrbinary('RETR README', open('README', 'wb').write)
            '226 Transfer complete.'
            >>> ftp.quit()
            >>> from ftplib import FTP_TLS
            >>> ftps = FTP_TLS('ftp.python.org')
            >>> ftps.login()           # login anonymously before securing control channel
            >>> ftps.prot_p()          # switch to secure data connection
            >>> ftps.retrlines('LIST') # list directory content securely
            total 9
            drwxr-xr-x   8 root     wheel        1024 Jan  3  1994 .
            drwxr-xr-x   8 root     wheel        1024 Jan  3  1994 ..
            drwxr-xr-x   2 root     wheel        1024 Jan  3  1994 bin
            drwxr-xr-x   2 root     wheel        1024 Jan  3  1994 etc
            d-wxrwxr-x   2 ftp      wheel        1024 Sep  5 13:43 incoming
            drwxr-xr-x   2 root     wheel        1024 Nov 17  1993 lib
            drwxr-xr-x   6 1094     wheel        1024 Sep 13 19:07 pub
            drwxr-xr-x   3 root     wheel        1024 Jan  3  1994 usr
            -rw-r--r--   1 root     root          312 Aug  1  1994 welcome.msg
            '226 Transfer complete.'
            >>> ftps.quit()
            >>>
            #
            # >>> ftp.retrbinary( 'RETR README', open('README', 'wb').write)

            If I type the above command it gives me an error

            Comment

            • cr4sh
              Banned
              New Member
              • Jul 2010
              • 20

              #7
              what error? when i run it, it works just fine

              Comment

              • Amit Vaity
                New Member
                • Jul 2010
                • 5

                #8
                Thnx for the help....It works fine....There were some errors in my code...

                Comment

                • cr4sh
                  Banned
                  New Member
                  • Jul 2010
                  • 20

                  #9
                  you are always welcome, let me know if anything else comes up.
                  Sincerely.

                  Comment

                  Working...