Help get this topic noticed by sharing it on Twitter, Facebook, or email.
I’m frustrated

Is the sample code available for download for Twisted Network Programming Essentials 2nd edition.

I am trying to work out the bugs in example 9-1 [pg.82]. I can not tell if the indentation from page 83 to page 84 is correct. I would like to be able to view the authors exact example code.

- Erik

My typed code if someone would like to look.

from zope.interface import implements, Interface

from twisted.cred import checkers, credentials, portal
from twisted.internet import protocol, reactor
from twisted.protocols import basic

class IProtocolAvatar(Interface):
def logout():
Clean up per-login resource allocated to this avatar.

class EchoAvatar(object):

def logout(self):

class Echo(basic.LineReceiver):
portal = None
avatar = None
logout = None

def connectionLost(self, reason):
if self.logout:
self.avatar = None
self.logout = None

def lineReceived(self, line):
if not self.avatar:
# print "line [32]: %s" % (line,)
username, password = line.strip().split(" ")
self.tryLogin(username, password)


def tryLogin(self, username, password):
self.portal.login(credentials.UsernamePassword(username, password), None, IProtocolAvatar).addCallbacks(self._cbLogin, self._ebLogin)

def _cbLogin(self, (interface, avatar, logout)):
self.avatar = avatar
self.logout = logout
self.sendLine("Login sucessful, please proceed.")

def _ebLogin(self, failure):
self.sendLine("Login denied, goodbye.")

class EchoFactory(protocol.Factory):
def __init__(self, portal):
self.portal = portal

def buildProtocol(self, addr):
proto = Echo()
proto.portal = self.portal
return proto

class Realm(object):

def requestAvatar(self, avatarId, mind, *interfaces):
if IProtocolAvatar in interfaces:
avatar = EchoAvatar()
return IPortocolAvatar, avatar, avatar.logout

raise NotImplementedError(
"This realm only supports the IPortocolAvatar interface.")

realm = Realm()
myPortal = portal.Portal(realm)
checker = checkers.InMemoryUsernamePasswordDatabaseDontUse()
checker.addUser("user", "pass")

reactor.listenTCP(8000, EchoFactory(myPortal))
1 person has
this question