@@ -0,0 +1,2807 @@
+import  paramiko%0Aimport os%0A%0Aclass SFTP(object):%0A    def __init__(self):%0A        super(SFTP,self).__init__()%0A        self._ssh = None%0A        self._sftp = None%0A        self._host = None%0A        self._port = None%0A        self._username = None%0A        self._password = None%0A    def _connect(self):%0A        self._ssh = paramiko.SSHClient()%0A        self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())%0A        self._ssh.connect(self._host,self._port,self._username,self._password)%0A    def _execute_command(self,command):%0A        stdin,stdout,stderr = self._ssh.exec_command(command)%0A        if stderr:%0A            raise Exception(%22exec remote command error: %25s%22%25stderr)%0A        else:%0A            return stdout%0A    def _openSftp(self):%0A        t = paramiko.Transport(sock=(self._host,self._port))%0A        t.connect(username=self._username,password=self._password)%0A        self._sftp = paramiko.SFTPClient.from_transport(t)%0A    def _sftp_get(self,remoteFile,localFile):%0A        if self._sftp:%0A            self._sftp.get(remoteFile,localFile)%0A        else:%0A            raise Exception(%22please open connection before sftp get.%22)%0A    def _sftp_put(self,localFile,remoteFile):%0A        if self._sftp:%0A            self._sftp.put(localFile,remoteFile)%0A        else:%0A            raise Exception(%22please open connection before sftp put%22)%0A    def _close(self):%0A        if self._ssh:%0A            self._ssh.close()%0A            self._ssh = None%0A%0A    def open_connection(self,host,port=22,username=%22root%22,password=%22arthur%22):%0A        if not isinstance(host,str) or not host:%0A            raise Exception(%22fail to open connection, host is %25s%22%25host)%0A        if not isinstance(port,int) or not port:%0A            raise Exception(%22fail to open connection, port is %25s%22%25host)%0A        if not isinstance(username,str) or not port:%0A            raise Exception(%22fail to open connection, port is %25s%22%25host)%0A        if not isinstance(password,str) or not port:%0A            raise Exception(%22fail to open connection, port is %25s%22%25host)%0A        self._host,self._port,self._username,self._password = host,port,username,password%0A        self._connect()%0A        self._openSftp()%0A    def put(self,localFile,remoteFile):%0A        if os.path.isfile(localFile):%0A%0A            self._sftp_put(localFile,remoteFile)%0A        elif os.path.isdir(localFile):%0A            self._execute_command(%22mkdir %25s -p%22%25remoteFile)%0A        else:%0A            raise Exception(%22fail to sftp put file(%25s) due to this file Not Found. %22%25localFile)%0A    def get(self,remoteFile,localFile):%0A        self._sftp_get(remoteFile,localFile)%0A    def close_connection(self):%0A        self._close()%0A%0A%0Aif __name__ == %22__main__%22:%0A    sftp = SFTP()%0A    sftp.open_connection(%22192.168.10.107%22,username=%22jarvan%22,password=%22newmedia%22)%0A    sftp.get(%22test%22,%22test%22)%0A    sftp.close_connection()%0A%0A

add remote