【问题标题】:"Unhandled Error" comes when TCP server tries to accept connections from client in twisted当 TCP 服务器尝试以扭曲的方式接受来自客户端的连接时,会出现“未处理的错误”
【发布时间】:2013-06-26 15:29:07
【问题描述】:
from twisted.internet.protocol import Factory,Protocol
from twisted.internet import reactor

class ChatServer(Protocol):
    def connectionMade(self):
        print("A Client Has Connected")

factory = Factory()
reactor.listenTCP(80,factory)
print("Chat Server Started")

reactor.run()

上面的代码运行成功。但是当我尝试打开 TCP(telnet localhost 80)。

发生错误:

Unhandled Error
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\twisted\python\log.py", line 69, 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 150, in _doReadOrWrite
    why = getattr(selectable, method)()
--- <exception caught here> ---
  File "C:\Python27\lib\site-packages\twisted\internet\tcp.py", line 718, in doRead
    protocol = self.factory.buildProtocol(self._buildAddr(addr))
  File "C:\Python27\lib\site-packages\twisted\internet\protocol.py", line 104, in buildProtocol
    p = self.protocol()
exceptions.TypeError: 'NoneType' object is not callable

如果有人知道解决方案,请帮助我。我刚接触扭曲。

【问题讨论】:

    标签: python twisted twisted.internet


    【解决方案1】:
    class ChatServer(Protocol):
        def connectionMade(self):
            print("A Client Has Connected")
    
    factory = Factory()
    reactor.listenTCP(80,factory)
    

    在这段代码中,您没有在 factoryChatServer 之间建立任何关联。尝试插入这一行:

    factory.protocol = ChatServer
    

    在即将发布(尚未发布)的 Twisted 版本中,Factory 正在获得一种新的类方法,以使此设置更加容易。使用那个版本,这个例子会更短:

    class ChatServer(Protocol):
        def connectionMade(self):
            print("A Client Has Connected")
    
    reactor.listenTCP(80, Factory.forProtocol(ChatServer))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 2022-01-20
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多