Python example.

Source: Internet
Author: User

# Ftptest. py-an example application using Python's ftplib module.
# Author: Matt Croydon <Matt@ooiio.com>, Referencing multiple sources, including:
# Pydoc for ftplib:Http://web.pydoc.org/2.2/ftplib.html
# Ftplib module docs:Http://www.python.org/doc/current/lib/module-ftplib.html
# Python Tutorial:Http://www.python.org/doc/current/tut/tut.html
# License: gnu gpl. The software is free, don't sue me.
# This was written under Python 2.2, though it shoshould work with Python 2.x and greater.

# Import the FTP object from ftplib
From ftplib import FTP

# This will handle the data being downloaded
# It will be explained shortly
Def handledownload (Block ):
File. Write (Block)
Print ".",

# Create an instance of the FTP object
# Optionally, you cocould specify username and password:
# Ftp ('hostname', 'username', 'Password ')
FTP = FTP ('ftp .cdrom.com ')

Print 'Welcome to Matt's ftplib example'
# Log in to the server
Print 'logging in .'
# You can specify username and password here if you like:
#FTP. login ('username', 'Password ')
# Otherwise, It defaults to anonymous
PrintFTP. Login()

# This is the directory that we want to go
Directory = 'pub/simtelnet/trumpet/Winsock'
# Let's change to that directory. You kids might call these 'folders'
Print 'Changing to '+ directory
FTP. CWD (directory)

# Print the contents of the Directory
FTP. retrlines ('LIST')

# Here's a file for us to play with. Remember trumpet Winsock?
Filename = 'winap 21f . Zip'

# Open the file for writing in binary mode
Print 'opening local file' + filename
File = open (filename, 'wb ')

# Download the file a chunk at a time
# Each Chunk is sent to handledownload
# We append the chunk to the file and then print a '.' For Progress
# RETR is an FTP command
Print 'getting' + filename
FTP. retrbinary ('retr'+ Filename, handledownload)

# Clean up time
Print 'closing file' + filename
File. Close ()

Print 'closing FTP connection'
PrintFTP. Close()

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.