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 &quot;Transport Created&quot;</div>
<div><br></div><div>    def verifyHostKey(self, pubKey, fingerprint):</div><div>        print &quot;In Verify Host Key&quot;</div><div>        return defer.succeed(True)</div><div><br></div><div>    def connectionSecure(self):</div>
<div>        print &quot;In Connection Secure&quot;</div><div>        temp = PasswordAuth(self.username, self.password, ClientConnection(self.command))</div><div>        print &quot;Created Everything&quot;</div><div>        self.requestService(temp)</div>
<div>        print &quot;Double Check&quot;</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 &quot;init password auth&quot;</div><div><br></div><div>    def getPassword(self, prompt=None):</div><div>        print &quot;In getPassword&quot;</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 &quot;Init Client Connection&quot;</div><div>        connection.SSHConnection.__init__(self)</div>
<div>        print &quot;Next line of Client Connection&quot;</div><div>        self.command = cmd</div><div><br></div><div>    def serviceStarted(self):</div><div>        print &quot;Service Started&quot;</div><div>        self.openChannel(CommandChannel(self.command, conn=self))</div>
<div><br></div><div>class CommandChannel(channel.SSHChannel):</div><div>    name = &#39;session&#39;</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 (&quot;Channel was opened&quot;)</div><div>        self.conn.sendRequest(self,&#39;exec&#39;,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(&quot;###### Entered Closed #####&quot;)</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>self.loseConnection()</div><div>        reactor.stop()</div><div>        print(&quot;##### Done with Closed ####&quot;)</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__ == &quot;__main__&quot;:</div><div>    import sys, getpass</div><div>    server = sys.argv[1]</div><div>    command = sys.argv[2]</div><div>    username = raw_input(&quot;Username: &quot;)</div>
<div>    password = getpass.getpass(&quot;Password: &quot;)</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 &quot;/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.py&quot;, line 586, in _doReadOrWrite</div><div>    why = selectable.doRead()</div>
<div>  File &quot;/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py&quot;, line 199, in doRead</div><div>    rval = self.protocol.dataReceived(data)</div><div>  File &quot;/usr/lib/python2.7/dist-packages/twisted/conch/ssh/transport.py&quot;, line 438, in dataReceived</div>
<div>    self.dispatchMessage(messageNum, packet[1:])</div><div>  File &quot;/usr/lib/python2.7/dist-packages/twisted/conch/ssh/transport.py&quot;, line 460, in dispatchMessage</div><div>    messageNum, payload)</div><div>
--- &lt;exception caught here&gt; ---</div><div>  File &quot;/usr/lib/python2.7/dist-packages/twisted/python/log.py&quot;, line 84, in callWithLogger</div><div>    return callWithContext({&quot;system&quot;: lp}, func, *args, **kw)</div>
<div>  File &quot;/usr/lib/python2.7/dist-packages/twisted/python/log.py&quot;, line 69, in callWithContext</div><div>    return context.call({ILogContext: newCtx}, func, *args, **kw)</div><div>  File &quot;/usr/lib/python2.7/dist-packages/twisted/python/context.py&quot;, line 118, in callWithContext</div>
<div>    return self.currentContext().callWithContext(ctx, func, *args, **kw)</div><div>  File &quot;/usr/lib/python2.7/dist-packages/twisted/python/context.py&quot;, line 81, in callWithContext</div><div>    return func(*args,**kw)</div>
<div>  File &quot;/usr/lib/python2.7/dist-packages/twisted/conch/ssh/service.py&quot;, line 44, in packetReceived</div><div>    return f(packet)</div><div>  File &quot;/usr/lib/python2.7/dist-packages/twisted/conch/ssh/connection.py&quot;, line 314, in ssh_CHANNEL_REQUEST</div>
<div>    channel = self.channels[localChannel]</div><div>exceptions.KeyError: 0</div></div><div><br></div>