ftplib 简单使用

# -*- coding: utf-8 -*-
#!/usr/bin/python
# Filename: ftp_client.py

from ftplib import FTP
import sys
import os

global f_obj

def update(path):
    global f_obj
    file = 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)
    except:
        print 'read file error!'

def download(path, outfile=None):
    global f_obj
    if outfile is None:
        outfile = sys.stdout
    dir = 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>>'
        return
    try:
        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)
    except:
        print 'download error!'
    print 'download success!'
def func_cmd(cmd, outfile = None):
    global f_obj
    if outfile is None:
        outfile = sys.stdout

    if 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 list   
        except:
            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)
        except:
            print 'error'
    elif cmd == '7':
        try:
            path = raw_input('please input the dir>>')
            f_obj.mkd(path)
        except:
            print 'error'
    elif cmd == '8':
        try:
            dir = raw_input('please input the dir which you want remove>>')
            f_obj.rmd(dir)
        except:
            print 'error'
    elif cmd == '9':
        try:
            file = raw_input('please input the filename>>')
            f_obj.size(file)
        except:
            print 'error'
    elif cmd == '10':
        f_obj.quit()
        f_obj.close()
        print '结束连接......'
        sys.exit(0)
    else:
        print 'no command!'

    raw_input('enter any key continue.....')


def main():
    global f_obj
    host = '192.168.1.12'
    user = 'tomy'
    password = '111111'

    try:
        print 'Connect Host PC......'
        f_obj = FTP(host)
        print f_obj.getwelcome()
    except:
        print "Can't reach the Ftp host!"
        sys.exit(1)   

    try:
        print 'login by user.....'
        f_obj.login('19930', '111111')
    except:
        print "Can't login!"
        sys.exit(1)

    while(1):
        intro_str = '''
        All commands as:
        1:显示当前文件夹文件列表(含有详细列表信息)
        2:显示当前文件夹文件
        3:上传文件
        4:下载文件
        5:显示当前目录
        6:跳转进入某目录
        7:创建一个新的目录
        8:删除一个目录
        9:返回一个文件的文件大小
        10:退出FTP
        '''

        print intro_str
        cmd = raw_input('please input cmd>>')
        func_cmd(cmd)

if __name__ == '__main__':
    main()