Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 751|回复: 0

python向ftp上传文件,解决中文问题

[复制链接]
  • TA的每日心情
    奋斗
    2024-11-24 15:47
  • 签到天数: 804 天

    [LV.10]以坛为家III

    2053

    主题

    2111

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    726782
    发表于 2021-6-25 07:53:30 | 显示全部楼层 |阅读模式
    # coding: UTF-8
    import os
    import sys
    import salt.client
    import salt.config
    import time
    from ftplib import FTP, FTP_TLS
    import json
    from subprocess import Popen, PIPE, STDOUT
    
    agent_ids = agent_ip.split(',')
    if '{' in sys_type:
        sys_types=json.loads(sys_type)
    else:
        sys_types={}
        for i in agent_ids:
            sys_types.update({i:sys_type})
    
    
    def get_source_file_path(source_file_path,agent_id):
        global sys_types
        sys_type=sys_types.get(agent_id)
        if sys_type == 'windows':
            if source_file_path.startswith('/'):
                source_file_path='c:'+source_file_path
            source_file_path = source_file_path.replace('\\\\', '/')
            source_file_path = source_file_path.replace('\\', '/')
            source_file_path=source_file_path.decode('gbk')
            print source_file_path
        else:
            if isinstance(source_file_path, unicode):
                source_file_path = source_file_path.encode('utf8')
        file_paths = source_file_path.split('\n')
        return file_paths
    
    if isinstance(ftp_dir, unicode):
        ftp_dir = ftp_dir.encode('utf8')
    
    BASE_URL = os.getenv('ANT_BASEURL')
    
    fail = []
    success = []
    
    
    def agent_to_proxy():
        global agent_ids, source_file_path, success, fail, BASE_URL
        proxy_path = os.getenv('ANT_MODULE_ROOT', '')
        master_conf = os.path.join(proxy_path, 'conf', 'master')
        if not os.path.exists(master_conf):
            print('Proxy 路径不正确 {}'.format(proxy_path))
            sys.exit(1)
        opts = salt.config.client_config(master_conf)
        LocalClient = salt.client.LocalClient(master_conf)
        CACHE_DIR = opts['cachedir']
        for agent_id in agent_ids:
            file_paths=get_source_file_path(source_file_path, agent_id)
            print file_paths
            ret = LocalClient.cmd(agent_id, 'cp.push', file_paths, expr_form='list')
            no_exist_hosts = set(ret.keys()) - set(agent_ids)
            print no_exist_hosts
            if no_exist_hosts:
                print('异常:{} agent 不存在'.format(list(no_exist_hosts)))
                sys.exit(1)
            for file_path in file_paths:
                for host, status in ret.iteritems():
                    if status is True:
                        proxy_file_path = os.path.join(CACHE_DIR, 'minions', host, 'files',
                                                       file_path.lstrip('/'))
                        res = proxy_to_ftp(proxy_file_path)
                        if res:
                            print file_path, '上传成功'
                        else:
                            fail.append('{}:{}'.format(host, file_path))
                    elif status is False:
                        print('{}:{} 上传失败,可能文件不存在'.format(host, file_path))
                        fail.append('{}:{}'.format(host, file_path))
                    else:
                        print('异常:{}:{} 上传失败,{}'.format(host, file_path, status))
                        fail.append('{}:{}'.format(host, file_path))
    
    
    def _conn_ftp(user, passwd):
        global ftp_host, ftp_port, connect_type
        conn = False
    
        ispasv = False if user in (None, 'false') else True
    
        ftp_url = ftp_host
        connect_type = connect_type.lower()
        port = ftp_port
        try:
            if ':' in ftp_url:
                conn = True
                ftp_url, port = ftp_url.split(':')
    
            if conn:
                ftp = FTP()
                ftp.connect(host=str(ftp_url), port=int(port))
            else:
                ftp = FTP(ftp_url)
            ftp.login(user, passwd)
    
        except Exception as e:
            if 'requires SSL.' in '{}'.format(e):
                try:
                    if conn:
                        ftp = FTP_TLS()
                        ftp.connect(host=ftp_url, port=int(port))
                    else:
                        ftp = FTP_TLS(ftp_url)
                    ftp.login(user, passwd)
                except Exception as e:
                    print e
                    sys.exit(1)
            else:
                print e
                sys.exit(1)
        return ftp, ispasv
    
    
    def proxy_to_ftp(proxy_file_path):
        global username, password, ftp_dir
        ftp, ispasv = _conn_ftp(username, password)
        ftp.encoding = "utf-8"
        try:
            ftp.set_pasv(ispasv)
            # ftp.retrlines('LIST')
        except:
            ftp.set_pasv(not ispasv)
        now_path = ftp.pwd()
        print now_path
    
        if ftp_dir and ftp_dir != '/':
            try:
                ftp.cwd(ftp_dir)
            except:
                new_path = os.path.join(now_path, ftp_dir.lstrip('/'))
                print new_path
                ftp.mkd(new_path)
                ftp.cwd(new_path)
    
        try:
            ftp.rename(os.path.basename(proxy_file_path).decode("utf8").encode('gbk'), os.path.basename(proxy_file_path).decode("utf8").encode('gbk')+'.'+time.strftime('%Y-%m-%d_%H:%M:%S'))
        except:
            pass
        print proxy_file_path
        with open(proxy_file_path, 'rb') as f_:
            result = ftp.storbinary('STOR %s' % os.path.basename(proxy_file_path).decode("utf8").encode('gbk'), f_)
    
        ftp.close()
        if 'Transfer complete.' in result:
            return True
        else:
            return False
    
    
    try:
        agent_to_proxy()
        if success:
            print '文件传输成功:{}'.format(','.join(success))
        if fail:
            print '文件传输失败:{}'.format(','.join(fail))
            sys.exit(1)
    except Exception as e:
        print e
        sys.exit(1)
    

      

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-12-22 22:52 , Processed in 0.056348 second(s), 28 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表