[Checkins] SVN: Sandbox/J1m/conch/echo_ Got rid of the use of sessions. Now we have custom channels that send
Jim Fulton
jim at zope.com
Wed Jul 29 18:40:35 EDT 2009
Log message for revision 102367:
Got rid of the use of sessions. Now we have custom channels that send
data back and forth directly.
Changed:
U Sandbox/J1m/conch/echo_client.py
U Sandbox/J1m/conch/echo_server.py
-=-
Modified: Sandbox/J1m/conch/echo_client.py
===================================================================
--- Sandbox/J1m/conch/echo_client.py 2009-07-29 18:45:43 UTC (rev 102366)
+++ Sandbox/J1m/conch/echo_client.py 2009-07-29 22:40:35 UTC (rev 102367)
@@ -1,6 +1,5 @@
import twisted.conch.ssh.channel
-import twisted.conch.ssh.common
import twisted.conch.ssh.connection
import twisted.conch.ssh.keys
import twisted.conch.ssh.transport
@@ -35,18 +34,13 @@
def serviceStarted(self):
self.openChannel(Channel(conn = self))
+# Channels are twisted.internet.interfaces.ITransports
class Channel(twisted.conch.ssh.channel.SSHChannel):
- name = 'session'
+ name = 'echo'
def channelOpen(self, data):
- d = self.conn.sendRequest(
- self, 'shell', '',
- wantReply = 1)
- d.addCallback(self._cbSendRequest)
self.catData = ''
-
- def _cbSendRequest(self, ignored):
self.write('This data will be echoed back to us by "cat."\r\n')
self.conn.sendEOF(self)
self.loseConnection()
Modified: Sandbox/J1m/conch/echo_server.py
===================================================================
--- Sandbox/J1m/conch/echo_server.py 2009-07-29 18:45:43 UTC (rev 102366)
+++ Sandbox/J1m/conch/echo_server.py 2009-07-29 22:40:35 UTC (rev 102367)
@@ -3,14 +3,12 @@
import twisted.cred.portal
import twisted.conch.avatar
import twisted.conch.checkers
+import twisted.conch.ssh.channel
import twisted.conch.ssh.factory
import twisted.conch.ssh.userauth
import twisted.conch.ssh.connection
import twisted.conch.ssh.keys
-import twisted.conch.ssh.session
import twisted.internet.reactor
-import twisted.internet.protocol
-import twisted.python.components
import twisted.python.log
import zope.interface
@@ -20,13 +18,27 @@
log in with username "user" and password "password".
"""
+# Channels are twisted.internet.interfaces.ITransports.
+# We would normally associate a protocol.
+class Channel(twisted.conch.ssh.channel.SSHChannel):
+
+ name = 'echo'
+
+ def dataReceived(self, data):
+ if data == '\r':
+ data = '\r\n'
+ elif data == '\x03': #^C
+ self.loseConnection()
+ return
+ self.write(data.upper())
+
class ExampleAvatar(twisted.conch.avatar.ConchUser):
def __init__(self, username):
twisted.conch.avatar.ConchUser.__init__(self)
self.username = username
self.channelLookup.update(
- {'session':twisted.conch.ssh.session.SSHSession}
+ {'echo': Channel}
)
class ExampleRealm:
@@ -35,18 +47,6 @@
def requestAvatar(self, avatarId, mind, *interfaces):
return interfaces[0], ExampleAvatar(avatarId), lambda: None
-class EchoProtocol(twisted.internet.protocol.Protocol):
- """this is our example protocol that we will run over SSH
- """
- def dataReceived(self, data):
- if data == '\r':
- data = '\r\n'
- elif data == '\x03': #^C
- self.transport.loseConnection()
- return
- self.transport.write(data.upper())
-
-
user_pubkey = twisted.conch.ssh.keys.Key.fromFile('ukey.pub')
class InMemoryPublicKeyChecker(twisted.conch.checkers.SSHPublicKeyDatabase):
@@ -55,34 +55,6 @@
return (credentials.username == 'user' and
user_pubkey.blob() == credentials.blob)
-class ExampleSession:
-
- def __init__(self, avatar):
- """
- We don't use it, but the adapter is passed the avatar as its first
- argument.
- """
-
- def getPty(self, term, windowSize, attrs):
- pass
-
- def execCommand(self, proto, cmd):
- raise Exception("no executing commands")
-
- def openShell(self, trans):
- ep = EchoProtocol()
- ep.makeConnection(trans)
- trans.makeConnection(twisted.conch.ssh.session.wrapProtocol(ep))
-
- def eofReceived(self):
- pass
-
- def closed(self):
- pass
-
-twisted.python.components.registerAdapter(
- ExampleSession, ExampleAvatar, twisted.conch.ssh.session.ISession)
-
class ExampleFactory(twisted.conch.ssh.factory.SSHFactory):
publicKeys = {
'ssh-rsa': twisted.conch.ssh.keys.Key.fromFile('skey.pub')
More information about the Checkins
mailing list