def testStor(self): # Connect client = ftp.FTPClient(passive=self.passive) client.debug = 1 factory = ClientFactory() factory.noisy = 0 factory.buildProtocol = lambda s, c=client: c reactor.connectTCP('localhost', self.ftp_port, factory) expectedContent = "Hello\n"*4 def gotResult(c): c.write(expectedContent) c.finish() def gotErr(f): self.errback(f) t = client.storeFile("HelloThere") t[0].addCallbacks(gotResult, gotErr) t[1].addCallbacks(self.callback, self.errback) # Wait for a result id = reactor.callLater(5, self.errback, "timed out") # timeout so we don't freeze while not hasattr(self, 'result') and not hasattr(self, 'error'): reactor.iterate() try: id.cancel() except ValueError: pass error = getattr(self, 'error', None) if error: raise error[0], error[1], error[2] self.assertEquals(open('HelloThere').read(), expectedContent)
def testShortFileListings(self): # Connect client = ftp.FTPClient(passive=self.passive) factory = ClientFactory() factory.noisy = 0 factory.buildProtocol = lambda s, c=client: c reactor.connectTCP('localhost', self.ftp_port, factory) # Issue the command and set the callbacks p = BufferingProtocol() d = client.nlst('.', p) d.addCallbacks(self.callback, self.errback) # Wait for the result id = reactor.callLater(5, self.errback, "timed out") # timeout so we don't freeze while not hasattr(self, 'result') and not hasattr(self, 'error'): reactor.iterate() try: id.cancel() except ValueError: pass error = getattr(self, 'error', None) if error: raise error[0], error[1], error[2] # Check that the listing contains this file (ftp_crap) filenames = p.buf.getvalue().split('\r\n') self.failUnless('ftp_crap' in filenames, 'ftp_crap not in file listing')
def testRetr(self): # Connect client = ftp.FTPClient(passive=self.passive) factory = ClientFactory() factory.noisy = 0 factory.buildProtocol = lambda s, c=client: c reactor.connectTCP('localhost', self.ftp_port, factory) # download ftp_crap proto = BufferingProtocol() d = client.retr(os.path.basename('ftp_crap'), proto) d.addCallbacks(self.callback, self.errback) # Wait for a result id = reactor.callLater(5, self.errback, "timed out") # timeout so we don't freeze while not hasattr(self, 'result') and not hasattr(self, 'error'): reactor.iterate() try: id.cancel() except ValueError: pass error = getattr(self, 'error', None) if error: raise error[0], error[1], error[2] # Check that the file is the same as read directly off the disk self.failUnless(type(self.result) == types.ListType, 'callback result is wrong type: ' + str(self.result)) data = proto.buf.getvalue() self.failUnless(data == open('ftp_crap', "rb").read(), 'RETRieved file does not match original')
except Exception, e: self.callbackException = sys.exc_info() raise errors = [None, None] def gotError(failure, x, errors_=errors): errors_[x] = failure # These LIST commands should should both fail d = client.list('.', ftp.FTPFileListProtocol()) d.addCallbacks(badResult, gotError, errbackArgs=(0,)) d = client.list('.', ftp.FTPFileListProtocol()) d.addCallbacks(badResult, gotError, errbackArgs=(1,)) factory = ClientFactory() factory.noisy = 0 factory.buildProtocol = lambda s,c=client: c reactor.connectTCP('localhost', self.ftp_port, factory) while None in errors and not self.callbackException: reactor.iterate() if self.callbackException: ce = self.callbackException raise ce[0], ce[1], ce[2] log.flushErrors(ftp.ConnectionLost) class FTPPassiveClientAndServerTests(FTPClientAndServerTests): """Identical to FTPClientTests, except with passive transfers.