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

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

On Tue, Apr 14, 2015 at 6:47 AM, halidou SANFO <sanfo.halidou@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 reactor from twisted.internet.defer import Deferred
from twisted.conch.ssh.common import NS import twisted.conch.scripts.cftp.ClientOptions from twisted.conch.scripts.cftp import ClientOptions from twisted.conch.ssh.filetransfer import FileTransferClient from twisted.conch.client.connect import connect from twisted.conch.client.default import SSHUserAuthClient, verifyHostKey from twisted.conch.ssh.connection import SSHConnection from twisted.conch.ssh.channel import SSHChannel
from twisted.conch.ssh import keys, userauth from 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

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@gmail.com> a écrit : On Tue, Apr 14, 2015 at 6:47 AM, halidou SANFO <sanfo.halidou@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@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

On 14 Apr, 11:47 am, sanfo.halidou@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'''
Hi Sanfo, You may want to be a little more careful to preserve ownership/authorship information. Your presentation of this code, particularly with an "author" tag including only your name, gives a strong impression that you are the author and copyright holder of this program. Doing this sort of thing can get you into some trouble. You may want to familiarise yourself with stackoverflow's terms of service and ask someone where you're interning to give you a basic overview of copyright law as it pertains to software. Good luck, Jean-Paul

HiThank you.I forgot to delete the default add of eclipse.Sorry!! sanfo Le Mercredi 15 avril 2015 13h13, "exarkun@twistedmatrix.com" <exarkun@twistedmatrix.com> a écrit : On 14 Apr, 11:47 am, sanfo.halidou@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'''
Hi Sanfo, You may want to be a little more careful to preserve ownership/authorship information. Your presentation of this code, particularly with an "author" tag including only your name, gives a strong impression that you are the author and copyright holder of this program. Doing this sort of thing can get you into some trouble. You may want to familiarise yourself with stackoverflow's terms of service and ask someone where you're interning to give you a basic overview of copyright law as it pertains to software. Good luck, Jean-Paul _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (3)
-
exarkun@twistedmatrix.com
-
halidou SANFO
-
Kevin Horn