modified transport/file/sftp.py to establish a connection to a server and get file upload running.

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2768 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
scrat 2008-07-21 19:57:38 +00:00
parent 29a8361c28
commit bfd6191683
4 changed files with 17 additions and 22 deletions

View file

@ -7,12 +7,12 @@ from cybertools.agent.transport.file.sftp import FileTransfer
def output(x): def output(x):
print x print x
ft = FileTransfer('cy05.de', 22, 'scrat', '...') ft = FileTransfer('cy05.de', 22, 'scrat', 'pyjmfha')
d = ft.upload('d:\\text.txt', 'text.txt') d = ft.upload('d:\\text2.rtf', 'text.txt')
d.addCallback(output) d.addCallback(output)
reactor.callLater(3, ft.close) reactor.callLater(21, ft.close)
reactor.callLater(4, reactor.stop) reactor.callLater(32, reactor.stop)
reactor.run() reactor.run()

View file

@ -13,3 +13,4 @@ transport.remote.url = 'http://localhost:8123'
transport.remote.ftp.url = 'http://cy05.de' transport.remote.ftp.url = 'http://cy05.de'
transport.remote.ftp.user = 'scrat' transport.remote.ftp.user = 'scrat'
transport.remote.sftp = 'testing' transport.remote.sftp = 'testing'
transport.remote.chunksize = 4096

View file

@ -26,12 +26,11 @@ from twisted.conch.ssh import channel, common, connection
from twisted.conch.ssh import filetransfer, transport, userauth from twisted.conch.ssh import filetransfer, transport, userauth
from twisted.internet import defer, protocol, reactor from twisted.internet import defer, protocol, reactor
CHUNKSIZE = 8096 CHUNKSIZE = 4096
class FileTransfer(protocol.ClientFactory): class FileTransfer(protocol.ClientFactory):
""" Transfers files to a remote SCP/SFTP server. """ Transfers files to a remote SCP/SFTP server.
""" """
channel = None channel = None
def __init__(self, host, port, username, password): def __init__(self, host, port, username, password):
@ -72,6 +71,8 @@ class SFTPChannel(channel.SSHChannel):
""" """
name = 'session' name = 'session'
remFile = ''
remOffset = 0
def channelOpen(self, data): def channelOpen(self, data):
d = self.conn.sendRequest(self, 'subsystem', common.NS('sftp'), wantReply=1) d = self.conn.sendRequest(self, 'subsystem', common.NS('sftp'), wantReply=1)
@ -105,31 +106,24 @@ class SFTPChannel(channel.SSHChannel):
d.addCallbacks(self.writeChunk, self.logError) d.addCallbacks(self.writeChunk, self.logError)
def writeChunk(self, remoteFile): def writeChunk(self, remoteFile):
if isinstance(remoteFile, tuple) == False:
self.remFile = remoteFile
data = self.localFile.read(CHUNKSIZE) data = self.localFile.read(CHUNKSIZE)
if len(data) < CHUNKSIZE: if len(data) < CHUNKSIZE:
# write rest self.d = self.remFile.writeChunk(self.remOffset, data)
print "[DEBUG] **** WRITING REMAINING CHUNK\n"
print "[DEBUG] **** len(data): ", len(data)
print "[DEBUG] **** data: %s \n" %(data)
self.d = remoteFile.writeChunk(len(data), data)
self.d.addCallbacks(self.finished, self.logError) self.d.addCallbacks(self.finished, self.logError)
print "[DEBUG] **** WRITING CHUNK\n" else:
#self.d = remoteFile.writeChunk(CHUNKSIZE, data) self.d = self.remFile.writeChunk(self.remOffset, data)
#self.d.addCallbacks(self.writeChunk, self.logError) self.remOffset = self.remOffset + CHUNKSIZE
d = remoteFile.writeChunk(CHUNKSIZE, data) self.d.addCallbacks(self.writeChunk, self.logError)
d.addCallbacks(self.writeChunk, self.logError)
def logError(self, reason): def logError(self, reason):
print 'error', reason print 'error', reason
def finished(self, result): def finished(self, result):
#self.deferred.callback('finished')
print "[DEBUG] **** finished has been called\n"
print "[DEBUG] **** result: ", result
self.localFile.close() self.localFile.close()
self.remFile.close()
self.d.callback('finished') self.d.callback('finished')
#result.callback('finished')
# classes for managing the SSH protocol and connection # classes for managing the SSH protocol and connection

View file

@ -73,7 +73,7 @@ class Transporter(QueueableAgent):
""" """
#return self.server.callRemote('getMetadata', resource.metadata) #return self.server.callRemote('getMetadata', resource.metadata)
self.deferred = defer.Deferred() self.deferred = defer.Deferred()
print "**** RESOURCE.PATH: ", resource.path #print "**** RESOURCE.PATH: ", resource.path
remoteFile = os.path.basename(resource.path) remoteFile = os.path.basename(resource.path)
d = self.ftpServer.upload(resource.path, remoteFile) d = self.ftpServer.upload(resource.path, remoteFile)
#d = self.server.callRemote('getMetadata', resource.metadata) #d = self.server.callRemote('getMetadata', resource.metadata)