[英]Python Unhandled Error Traceback (most recent call last):
This is my code, I was looking for answers but none resolve my problem. 这是我的代码,我在寻找答案,但没有一个能解决我的问题。 I'm new to Twisted and still learning.
我是Twisted的新手,仍然在学习。
Why do I get this error after I run telnet localhost 72
in the Windows command prompt? 在Windows命令提示符下运行
telnet localhost 72
后,为什么会出现此错误?
This is my code: 这是我的代码:
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()
the error message: 错误消息:
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
@firman Are you sure you're giving us the EXACT code that's causing issues? @firman您确定要给我们提供引起问题的精确代码吗? The code above (if it were written properly) cannot possibly give that error.
上面的代码(如果编写正确)可能无法给出该错误。 The error occurs in the
buildProtocol
function of the Factory
class, which you overloaded in your example. 该错误发生在
Factory
类的buildProtocol
函数中,您在示例中对其进行了重载。 I've edited your question and corrected some syntax errors. 我已经编辑了您的问题,并更正了一些语法错误。 Try it again and see if it works.
再试一次,看看是否可行。 If not then post your code that's giving you errors.
如果没有,请发布您的代码,这会给您带来错误。
The error you got is caused when you don't set the protocol
variable in your factory object and it remains None
. 您未在工厂对象中设置
protocol
变量而导致的错误,它仍然是None
。 For example, let's comment out the buildProtocol
function in your factory class so that it looks like this: 例如,让我们在工厂类中注释掉
buildProtocol
函数,使其看起来像这样:
class ChatFactory(Factory):
def __init__(self):
self.users = {}
# def buildProtocol(self, addr):
# return chatprotocol(self)
I get the error you originally got: 我收到您最初遇到的错误:
--- <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
To fix this issue, you can do 1 of 2 things. 要解决此问题,您可以做2件事情之一。 You can overload
buildProtocol()
to return the protocol object you want (you did this in your original example). 您可以重载
buildProtocol()
以返回所需的协议对象(在原始示例中已完成此操作)。 Or you can set the factory object to a variable, then set the protocol
class variable. 或者,您可以将工厂对象设置为变量,然后设置
protocol
类变量。 So your main method should look like: 因此,您的主要方法应如下所示:
factory = ChatFactory()
factory.protocol = chatprotocol
reactor.listenTCP(72, factory)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.