[英]Python Unhandled Error Traceback (most recent call last):
這是我的代碼,我在尋找答案,但沒有一個能解決我的問題。 我是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
@firman您確定要給我們提供引起問題的精確代碼嗎? 上面的代碼(如果編寫正確)可能無法給出該錯誤。 該錯誤發生在Factory
類的buildProtocol
函數中,您在示例中對其進行了重載。 我已經編輯了您的問題,並更正了一些語法錯誤。 再試一次,看看是否可行。 如果沒有,請發布您的代碼,這會給您帶來錯誤。
您未在工廠對象中設置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
要解決此問題,您可以做2件事情之一。 您可以重載buildProtocol()
以返回所需的協議對象(在原始示例中已完成此操作)。 或者,您可以將工廠對象設置為變量,然后設置protocol
類變量。 因此,您的主要方法應如下所示:
factory = ChatFactory()
factory.protocol = chatprotocol
reactor.listenTCP(72, factory)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.