[Twisted-Python] Methods to upload and download file to a sftp server using twisted conch

halidou SANFO sanfo.halidou at yahoo.fr
Tue Apr 14 05:47:10 MDT 2015


Hi everybody,I am student and need help of any one.I am working on internship and I have to implement a SFTP client usign twisted conch module.I have program thoe code to establish th ssh tunnel using password authentication. I arrive to create directory with "MakeDirectory" method of FileTransfertClient class.But my objective is to upload and download files and the FileTransfertClient  class has no method [or as I konw ] to do that.
Can you help me with way to do the uploading and the downloding with twisted conch?
Here is my program.<code>'''Created on 8 avr. 2015
@author: hsanfo'''
from sys import stdout
from twisted.python.log import startLogging, err
from twisted.internet import reactorfrom twisted.internet.defer import Deferred
from twisted.conch.ssh.common import NSimport twisted.conch.scripts.cftp.ClientOptionsfrom twisted.conch.scripts.cftp import ClientOptionsfrom twisted.conch.ssh.filetransfer import FileTransferClientfrom twisted.conch.client.connect import connectfrom twisted.conch.client.default import SSHUserAuthClient, verifyHostKeyfrom twisted.conch.ssh.connection import SSHConnectionfrom twisted.conch.ssh.channel import SSHChannel
from twisted.conch.ssh import keys, userauthfrom twisted.internet import defer
class SFTPSession(SSHChannel):    name = 'session'
    def channelOpen(self, whatever):        d = self.conn.sendRequest(            self, 'subsystem', NS('sftp'), wantReply=True)        d.addCallbacks(self._cbSFTP)

    def _cbSFTP(self, result):        client = FileTransferClient()        client.makeConnection(self)        self.dataReceived = client.dataReceived        self.conn._sftp.callback(client)


class SFTPConnection(SSHConnection):        def serviceStarted(self):        self.openChannel(SFTPSession())

class ClientUserAuth(userauth.SSHUserAuthClient):    def getPassword(self, prompt = None):        #normal password authentication        print "PASSWORD AUTH"        return defer.succeed('test') # <-- YOUR PASSWORD
    def getGenericAnswers(self, name, instruction, prompts):        #interactive password authentication        print "INTERACTIVE AUTH"        response = ['']*len(prompts)        for i, p in enumerate(prompts):            try:                if('password' in p[0].lower()):                    response[i] = 'test' # <-- YOUR PASSWORD            except:                pass        #The response is always a sequence, and the length of it is always        #identical to the length of prompts        return defer.succeed(response)       
def sftp(user, host, port):    options = ClientOptions()        options['host'] = host    options['port'] = port    conn = SFTPConnection()    conn._sftp = Deferred()    auth = SSHUserAuthClient(user, options, conn)    #auth = ClientUserAuth(user, options, conn)    #connect(host, port, options, verifyHostKey, auth)    connect(host, port, options, None, auth)        return conn._sftp

def transfer(client):    d = client.makeDirectory('foobarbaz', {})    def cbDir(ignored):        print 'Made directory'    d.addCallback(cbDir)    return d

def main():    startLogging(stdout)
    user = 'test'    host = '192.168.29.129'    port = 22    d = sftp(user, host, port)    d.addCallback(transfer)    d.addErrback(err, "Problem with SFTP transfer")    d.addCallback(lambda ignored: reactor.stop())    reactor.run()

if __name__ == '__main__':    main()<code> Best regards
SANFO

       
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://twistedmatrix.com/pipermail/twisted-python/attachments/20150414/69aac660/attachment.html>


More information about the Twisted-Python mailing list