码迷,mamicode.com
首页 > 编程语言 > 详细

python之实现ftp上传下载代码(含错误处理)

时间:2015-11-18 23:02:08      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#python之实现ftp上传下载代码(含错误处理)
#http://www.cnblogs.com/kaituorensheng/p/4480512.html#_label2



import ftplib
import socket
import os

def ftpconnect(ftp_info):
    try:
        ftp = ftplib.FTP(ftp_info[0])
    except (socket.error, socket.gaierror):
        print "ERROR: cannot reach %s" % ftp_info[0]
        return None

    username = ftp_info[1]
    passwd = ftp_info[2]
    try:
        ftp.login(username, passwd)
    except ftplib.error_perm:
        print "ERROR: cannot login anonymously"
        ftp.quit()
        return None
    return ftp


if __name__ == "__main__":
    ftp_info = ["10.19.3.199", "fastupdate_amap", "@utonavi&A.map"]
    ftp = ftpconnect(ftp_info)
    if not ftp:
        exit(1)
    bufsize = 1024
    fname = "20150416113942674.tar.gz"
    fp = open(os.path.join(".", fname), wb)
    remotefile = os.path.join("/ADF++", fname)
    ftp.retrbinary("RETR " + remotefile, fp.write, bufsize)

    #是否有目录,没有就创建;有的话看是否有权限创建
    a = ftp.dir()
    try:
        ftp.cwd("jimi")
    except ftplib.error_perm:
        try:
            ftp.mkd("jimi")
        except ftplib.error_perm:
            print "you have no authority to make dir"
    finally:
        ftp.quit()
    

 

python之实现ftp上传下载代码(含错误处理)

标签:

原文地址:http://www.cnblogs.com/dengyg200891/p/4976040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!