Python未处理的错误回溯(最近一次调用最后一次):

时间:2016-10-15 11:30:43

标签: python twisted

这是我的代码,我正在寻找答案,但没有解决我的问题。 我是Twisted的新手,还在学习。

为什么在Windows命令提示符下运行telnet localhost 72后会出现此错误?

这是我的代码:

from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor


class chatprotocol(LineReceiver):
    def __init__(self, factory):
        self.factory = factory
        self.name = None
        self.state = "REGiSTER"

    def connectionMade(self):
        self.sendLine("What's your Name?")

    def connectionLost(self, reason):
        if self.name in self.factory.users:
            del self.factory.Users[self.name]
            self.broadcastMessage("%s has left channel." % (self.name,))

    def lineReceived(self, line):
        if self.state == "REGISTER":
            self.handle_REGISTER(line)
        else:
            self.handle_CHAT(line)

    def handle_REGISTER(self, name):
        if name in self.factory.users:
            self.sendLine("Name taken , please choose another")
            return
        self.sendline("welcome ,%s!" % (name,))
        self.broadcastMessage("%s has joined the channel." % (name,))
        self.name = name
        self.factory.users[name] = self
        self.state = "CHAT"

    def handle_CHAT(self, message):
        message = "<%s> %s" % (self.name, message)
        self.broadcastMessage(message)

    def broadcastMessage(self, message):
        for name, protocol in self.factory.users.iteritems():
            if protocol != self:
                protocol.sendLine(message)

class ChatFactory(Factory):
    def __init__(self):
        self.users = {}

    def buildProtocol(self, addr):
        return chatprotocol(self)


reactor.listenTCP(72, ChatFactory())
reactor.run()

错误消息:

Unhandled Error
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\twisted\python\log.py", line 84, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "C:\Python27\lib\site-packages\twisted\python\context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "C:\Python27\lib\site-packages\twisted\python\context.py", line 81, in callWithContext
    return func(*args,**kw)
  File "C:\Python27\lib\site-packages\twisted\internet\selectreactor.py", line 149, in _doReadOrWrite
    why = getattr(selectable, method)()
--- <exception caught here> ---
  File "C:\Python27\lib\site-packages\twisted\internet\tcp.py", line 1066, in doRead
    protocol = self.factory.buildProtocol(self._buildAddr(addr))
  File "C:\Python27\lib\site-packages\twisted\internet\protocol.py", line 131, in buildProtocol
    p = self.protocol()
exceptions.TypeError: 'NoneType' object is not callable

1 个答案:

答案 0 :(得分:0)

@firman您确定要向我们提供导致问题的确切代码吗?上面的代码(如果写得正确)不可能给出错误。错误发生在buildProtocol类的Factory函数中,您在示例中重载了该函数。我已编辑了您的问题并更正了一些语法错误。再试一次,看看它是否有效。如果没有,请发布给您错误的代码。

如果您未在工厂对象中设置protocol变量并且它仍为None,则会导致错误。例如,让我们在工厂类中注释掉buildProtocol函数,使其如下所示:

class ChatFactory(Factory):
    def __init__(self):
        self.users = {}

#    def buildProtocol(self, addr):
#        return chatprotocol(self)

我收到你最初得到的错误:

--- <exception caught here> ---
  File "/Projects/virtenv/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 1066, in doRead
    protocol = self.factory.buildProtocol(self._buildAddr(addr))
  File "/Projects/virtenv/local/lib/python2.7/site-packages/twisted/internet/protocol.py", line 131, in buildProtocol
    p = self.protocol()
exceptions.TypeError: 'NoneType' object is not callable

要解决此问题,您可以执行以下两项操作之一。您可以重载buildProtocol()以返回所需的协议对象(您在原始示例中执行了此操作)。或者您可以将工厂对象设置为变量,然后设置protocol类变量。因此,您的主要方法应如下所示:

factory = ChatFactory()
factory.protocol = chatprotocol
reactor.listenTCP(72, factory)