Python uses the ftplib block in the standard library as a small FTP client application

Source: Internet
Author: User

15:40:00

Yesterday, I spent some time reading the documentation and functions of the ftplib block. With the help of the example, I spent an hour creating an FTP client. Program . In general, the FTP process is relatively simple. In summary, it is: Connection -- logon -- operation -- disconnect Code The Code is as follows:

 

#-*-Coding: UTF-8 -*-#! /Usr/bin/Python # filename: ftp_client.pyfrom ftplib import ftpimport sysimport osglobal f_objdef Update (PATH): Global f_objfile = OS. path. split (PATH) [1] ext = OS. path. splitext (File) [1] Try: If ext in ('.txt ', '.htm', 'html'): f_obj.storline ('stor' + file, open (File) else: f_obj.storbinary ('stor' + file, open (file, 'rb'), 1024) handle T: Print 'read file error! 'Def download (path, OUTFILE = none): Global f_objif OUTFILE is none: OUTFILE = sys. stdoutdir = OS. path. split (PATH) [0] file = OS. path. split (PATH) [1] ext = OS. path. splitext (File) [1] default_dir = f_obj.pwd () If DIR is not none: f_obj.cwd (DIR) If Len (File) = 0: print 'Please input right file or path> 'returntry: If ext in ('.txt ', '.htm', '.html', '.png '): OUTFILE = open (file, 'w ') f_obj.retrlines ('RETR' + file, Lambda S, W = OUTFILE. Write: w (S + "/N") else: f_obj.retrbinary ('retr '+ file, OUTFILE. Write) failed T: Print 'Download error! 'Print 'Download success! 'Def func_cmd (CMD, OUTFILE = none): Global f_objif OUTFILE is none: OUTFILE = sys. stdoutif cmd = '1': Print 'current dir files as: 'f _ obj. dir () Elif cmd = '2': Try: Str = ''list = [] dir = raw_input ('input dir> ') List = f_obj.nlst (DIR) ''. join (list) print listparts T: Print 'error' Elif cmd = '3': print 'You Want to upload file to FTP! 'Filename = raw_input ('Please input filename or path> ') Update (filename) print 'upload succ! 'Elif cmd = '4': print 'you want to download files from TFP! 'Filename _ Path = raw_input ('Please input filename or path> ') download (filename_path) print' ------ download succ! ------ 'Elif cmd = '5': Print 'current dir: 'print f_obj.pwd () Elif cmd = '6': try: path = raw_input ('Please input the path> ') f_obj.cwd (PATH) handle T: Print 'error' Elif cmd = '7': try: path = raw_input ('Please input the dir> ') f_obj.mkd (PATH) failed T: Print 'error' Elif cmd = '8': try: dir = raw_input ('Please input the Dir which you want remove> ') f_obj.rmd (DIR) Comment T: Print 'error' Elif cmd = '9': Try: file = Ra W_input ('Please input The FILENAME> ') f_obj.size (File) before T: Print 'error' Elif cmd = '10': f_obj.quit () f_obj.close () print 'end connection ...... 'sys. exit (0) else: Print 'no command! 'Raw _ input ('enter any key continue ..... ') def main (): Global f_objhost =' 204. 45.67.107 'user = '000000' Password = '000000' try: Print 'connect host PC ...... 'F _ OBJ = FTP (host) print f_obj.getwelcome () handle T: Print "can't reach the FTP host! "Sys. exit (1) Try: Print 'login by user ..... 'F _ obj. login ('100', '100') failed T: Print "can't login! "Sys. exit (1) While (1): intro_str = ''' all commands as: 1: display the list of current folder files (including detailed list information) 2: display the current folder file 3: upload File 4: Download file 5: display current directory 6: jump to a directory 7: Create a new directory 8: delete a directory 9: return the file size of a file 10: exit FTP ''' print intro_strcmd = raw_input ('Please input cmd> ') func_cmd (CMD) If _ name _ =' _ main __': main ()

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.