我要读的是Jessica McKellar和Abe Fetting撰写的《扭曲的网络编程要点》第二版。我被困在以下扭曲的可信认证示例9-1中。我已经逐行,逐字母地仔细检查了我的代码。该代码将运行,当我与telnet客户端连接并输入“用户密码”时,它将无法登录。我只是收到未授权的消息。它本身的脚本不会生成任何错误消息或异常。

谢谢

import sys
from zope.interface import implements, Interface

from twisted.cred import checkers, credentials, portal
from twisted.internet import protocol, reactor
from twisted.protocols import basic
from twisted.python.log import startLogging; startLogging(sys.stdout)

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

class EchoAvatar(object):
    implements(IPortocolAvatar)

    def logout(self):
        pass

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

    def connectionLost(self, reason):
        if self.logout:
            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)

        else:
            self.sendLine(line)

    def tryLogin(self, username, password):
        self.portal.login(credentials.UsernamePassword(username, password), None, PortocolAvatar).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.")
        self.transport.loseConnection()

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):
    implements(portal.IRealm)

    def requestAvatar(self, avatarId, mind, *interfaces):
        if IPortocolAvatar 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")
myPortal.registerChecker(checker)

reactor.listenTCP(8000, EchoFactory(myPortal))
reactor.run()

最佳答案

您在tryLoginProtocolAvatar-> IProtocolAvatar)中有错字:

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


更新

还有另一种错字:IPortocolAvatar-> IProtocolAvatar

运行服务器后,请尝试以下测试客户端代码:

import telnetlib
import time

t = telnetlib.Telnet('localhost', 8000)
t.write(b'user pass\r\n')
t.write(b'blah blah\r\n')
time.sleep(1)
print(t.read_eager())

07-24 09:51