首页 > 编程语言 > 详细

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

时间:2015-11-18 23:01:56      阅读:1603      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!