Python implements a simple ftp client, pythonftp Client

Source: Internet
Author: User
Tags ftp client

Python implements a simple ftp client, pythonftp Client

This example describes how to implement a simple ftp client using python. Share it with you for your reference. The specific implementation method is as follows:

#! /Usr/bin/python #-*-coding: UTF-8-*-import ftplibimport osimport socketHOST = 'ftp .mozilla.org 'dirn = 'pub/mozilla.org/webtools'FILE = comment 'def writedata (data ): f = open (FILE, 'wb ') try: f. write (data) finally: f. close () def main (): try: f = ftplib. FTP (HOST) socket T (socket. error, socket. gaierror): print 'error: cannot reach "% s" '% HOST return print' *** Connected to host "% s" '% HOST try: f. login () Counter t ftplib. error_perm: print 'error: cannot login anonymously 'f. quit () return print '*** Logged in as "anonymously" 'Try: f. cwd (DIRN) effect ftplib. error_perm: print 'errorl cannot CD to "% s" '% DIRN f. quit () return print '*** Changed to "% s" folder' % DIRN try: # pass a callback function to retrbinary () it is called every time a binary data is received. retrbinary ('retr % s' % FILE, writedata) failed t ftplib. error_perm: print 'error: cannot read file "% s" '% FILE OS. unlink (FILE) else: print '*** Downloaded "% s" to CWD' % FILE f. quit () returnif _ name _ = '_ main _': main ()

I hope this article will help you with Python programming.

Related Article

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.