Hi everyone, I am a beginner to Twisted so any help would be appreciated. <div><br></div><div>I got an example code from the Twisted, Networking Programming Essentials book: </div><div><br></div><div><div>from twisted.conch import error</div>
<div>from twisted.conch.ssh import transport, connection, keys, userauth, channel, common</div><div>from twisted.internet import defer, protocol, reactor</div><div><br></div><div>class ClientCommandTransport(transport.SSHClientTransport):</div>
<div> def __init__(self, username, password, command):</div><div> self.username = username</div><div> self.password = password</div><div> self.command = command</div><div> print "Transport Created"</div>
<div><br></div><div> def verifyHostKey(self, pubKey, fingerprint):</div><div> print "In Verify Host Key"</div><div> return defer.succeed(True)</div><div><br></div><div> def connectionSecure(self):</div>
<div> print "In Connection Secure"</div><div> temp = PasswordAuth(self.username, self.password, ClientConnection(self.command))</div><div> print "Created Everything"</div><div> self.requestService(temp)</div>
<div> print "Double Check"</div><div><br></div><div>class PasswordAuth(userauth.SSHUserAuthClient):</div><div> def __init__(self, user, password, connection):</div><div> userauth.SSHUserAuthClient.__init__(self, user, connection)</div>
<div> self.password = password</div><div> print "init password auth"</div><div><br></div><div> def getPassword(self, prompt=None):</div><div> print "In getPassword"</div><div> return defer.succeed(self.password)</div>
<div><br></div><div>class ClientConnection(connection.SSHConnection):</div><div> def __init__(self, cmd, *args, **kwargs):</div><div> print "Init Client Connection"</div><div> connection.SSHConnection.__init__(self)</div>
<div> print "Next line of Client Connection"</div><div> self.command = cmd</div><div><br></div><div> def serviceStarted(self):</div><div> print "Service Started"</div><div> self.openChannel(CommandChannel(self.command, conn=self))</div>
<div><br></div><div>class CommandChannel(channel.SSHChannel):</div><div> name = 'session'</div><div><br></div><div> def __init__(self, command, *args, **kwargs):</div><div> channel.SSHChannel.__init__(self, *args, **kwargs)</div>
<div> self.command = command</div><div><br></div><div> def channelOpen(self, data):</div><div> print ("Channel was opened")</div><div> self.conn.sendRequest(self,'exec',common.NS(self.command),wantReply = True).addCallback(self._gotResponse)</div>
<div><br></div><div> def _gotResponse(self,_):</div><div> self.conn.sendEOF(self)</div><div><br></div><div> def dataReceived(self, data):</div><div> print data</div><div><br></div><div> def closed(self):</div>
<div> print("###### Entered Closed #####")</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>self.loseConnection()</div><div> reactor.stop()</div><div> print("##### Done with Closed ####")</div>
<div><br></div><div>class ClientCommandFactory(protocol.ClientFactory):</div><div> def __init__(self, username, password, command):</div><div> self.username = username</div><div> self.password = password</div>
<div> self.command = command</div><div><br></div><div> def buildProtocol(self,addr):</div><div> protocol = ClientCommandTransport(</div><div> self.username, self.password, self.command)</div><div>
return protocol</div><div><br></div><div>if __name__ == "__main__":</div><div> import sys, getpass</div><div> server = sys.argv[1]</div><div> command = sys.argv[2]</div><div> username = raw_input("Username: ")</div>
<div> password = getpass.getpass("Password: ")</div><div> factory = ClientCommandFactory(username, password, command)</div><div> reactor.connectTCP(server, 22, factory)</div><div> reactor.run()</div>
</div><div><br></div><div><br></div><div><br></div><div>*************************************************************</div><div><br></div><div>But after running this, I get this Unhandled error. Which is strange because the reactor is being successfully stopped. Does anyone have any clue what this means and how I should go about fixing it? Thanks!!!!!</div>
<div><br></div><div><div>Unhandled Error</div><div>Traceback (most recent call last):</div><div> File "/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 586, in _doReadOrWrite</div><div> why = selectable.doRead()</div>
<div> File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 199, in doRead</div><div> rval = self.protocol.dataReceived(data)</div><div> File "/usr/lib/python2.7/dist-packages/twisted/conch/ssh/transport.py", line 438, in dataReceived</div>
<div> self.dispatchMessage(messageNum, packet[1:])</div><div> File "/usr/lib/python2.7/dist-packages/twisted/conch/ssh/transport.py", line 460, in dispatchMessage</div><div> messageNum, payload)</div><div>
--- <exception caught here> ---</div><div> File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger</div><div> return callWithContext({"system": lp}, func, *args, **kw)</div>
<div> File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext</div><div> return context.call({ILogContext: newCtx}, func, *args, **kw)</div><div> File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext</div>
<div> return self.currentContext().callWithContext(ctx, func, *args, **kw)</div><div> File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext</div><div> return func(*args,**kw)</div>
<div> File "/usr/lib/python2.7/dist-packages/twisted/conch/ssh/service.py", line 44, in packetReceived</div><div> return f(packet)</div><div> File "/usr/lib/python2.7/dist-packages/twisted/conch/ssh/connection.py", line 314, in ssh_CHANNEL_REQUEST</div>
<div> channel = self.channels[localChannel]</div><div>exceptions.KeyError: 0</div></div><div><br></div>