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

halidou SANFO sanfo.halidou at yahoo.fr
Wed Apr 15 01:11:13 MDT 2015


Thank you for your reply.In fact I found this code in many forums like S.O and I tried to understand it, by doing exactly  all steps you recommend in you response. The difficulty I have found is with the method of the returned object of openFile method of the FileTransferClient class. This returned object that adhere to ISFTP interface; has two methods to write and read: "writeChunk" and "readChunk". My problem is to use these last functions or methods to perform a file transfert (uploading and downloading)  from my local machine running twisted to a SFTP server and vise-versa.Can you give me an code example to perform this task? with the methods of the openFile object.
Sanfo

 


        


     Le Mercredi 15 avril 2015 6h22, Kevin Horn <kevin.horn at gmail.com> a écrit :
   

 On Tue, Apr 14, 2015 at 6:47 AM, halidou SANFO <sanfo.halidou at yahoo.fr> wrote:

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




This is a multi-step process. 
1) Open the StackOverflow page that you copied the above code from.2) Actually read the whole answer.3) Read the documentation for the FileTransferClient class.  It's in the Twisted API documentation.  It does have an openFile method.4) Use the openFile method.5) read and/or write using the returned object, which (as explained in the SO answer) will adhere to the ISFTPFile interface.  That also has documentation, which you should probably read.6) Once you have done all those things, if you still have questions about _specific_ problems, feel free to ask them.


--Kevin Horn
_______________________________________________
Twisted-Python mailing list
Twisted-Python at twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20150415/431c91f7/attachment-0002.html>


More information about the Twisted-Python mailing list