
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