python实现上传FTP

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import paramiko
from urllib import quote

#Environment_XXX为上传FTP的服务器路径
Environment_root = '/home/appbao/app/shoushua_biz/iOS/'
Environment_text = '/home/appbao/app/shoushua_biz/iOS/test'
Environment_RC = '/home/appbao/app/shoushua_biz/iOS/rc'
Environment_alpha = '/home/appbao/app/shoushua_biz/iOS/alpha'
Environment_release = '/home/appbao/app/shoushua_biz/iOS/release'

#remote_XXX为服务器ipa下载路径
remote_head_text = 'http://172.16.132.241/app/shoushua_biz/iOS/test'
remote_head_RC = 'http://172.16.132.241/app/shoushua_biz/iOS/rc'
remote_head_alpha = 'http://172.16.132.241/app/shoushua_biz/iOS/alpha'
remote_head_release = 'http://172.16.132.241/app/shoushua_biz/iOS/release'

Environment = [Environment_text,Environment_RC,Environment_alpha,Environment_release]
remote_heads = [remote_head_text,remote_head_RC,remote_head_alpha,remote_head_release]

class SFTPClient:

    #init
    def __init__(self,host = None,userName = None,pwd = None):
        print '初始化完成'
        self.host = host
        self.userName = userName
        self.pwd = pwd
        print self.host + self.userName + self.pwd
        pass

    # creat SFTP
    def connectSFTP(self):
        # 创建SSH对象
        self.sftp = paramiko.SSHClient()
        self.sftp.set_missing_host_key_policy (paramiko.AutoAddPolicy ())
        self.sftp.connect(self.host,22, self.userName, self.pwd ,timeout=20)
        print 'SFTP连接完成'

    def execute(self, *cmd):
        self.files = []
        stdin, stdout, stderr = self.sftp.exec_command (*cmd)
        for x in stdout.readlines():
            print x
            self.files.append(x)
    #upload
    def uploadData(self,localpath = None,remotepath = None):
        try:
            trans = paramiko.Transport ((self.host, 22))
            # 建立连接
            trans.connect (username=self.userName, password=self.pwd)
            # 实例化一个 sftp对象,指定连接的通道
            sftp = paramiko.SFTPClient.from_transport (trans)
            # 发送文件
            sftp.put (localpath, remotepath)
            # 下载文件
            trans.close ()
        except Exception,e:
            print str(e) + 'SFTP上传失败'
        finally:
            print 'SFTP上传结束'

    #close
    def close(self):
        self.sftp.close()


if __name__ == '__main__':
    ftp = None
    try:
        ftp = SFTPClient (host='FTP地址', userName='用户名', pwd='密码')
        ftp.connectSFTP ()
    except Exception,e:
        print '连接失败'+str(e)
        exit(1)
    
    #获取远端服务器下对应的文件佳
    ftp.execute('cd /home/appbao/app/shoushua_biz/iOS/ && ls')
    remote = raw_input('选择一个文件夹或者重新创建一个文件夹\r\n')

    print '您选择了%s文件'%remote

    hasFiles = False
    for sub in ftp.files:
        if(sub.encode ('gbk') == remote):
            hasFiles = True
    if hasFiles == False:
        ftp.execute ('mkdir /home/appbao/app/shoushua_biz/iOS/%s' % remote)

    #获取文件名称
    filePath = raw_input('请输入本地文件\r\n')
    filePath = filePath.strip()
    filePath = quote(filePath)
    fileName = filePath.split('/')[-1]

    #上传
    ftp.uploadData(localpath=filePath,
                remotepath='%s%s/%s'%(Environment_root,remote,fileName))
    ftp.close()

    finalPath = 'http://172.16.132.241/app/shoushua_biz/iOS/%s/%s'%(remote,fileName)
    print '上传的远端地址\r\n%s'%finalPath

将此python执行加入到自定义命令

自定义终端执行>https://www.jianshu.com/p/5fcd173f6103

自定义命令代码举例

postFTP(){
python /XXX/XXX/XXX.py  #此处文件路径为你的python代码路径
}

你可能感兴趣的:(python实现上传FTP)