python - 当 TCP 服务器尝试接受来自扭曲的客户端的连接时,出现 "Unhandled Error"

标签 python twisted twisted.internet

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

如果有人知道解决方案,请帮助我。我是扭曲的新手。

最佳答案

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))

关于python - 当 TCP 服务器尝试接受来自扭曲的客户端的连接时,出现 "Unhandled Error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17371792/

相关文章:

python - 何时在 Python 中使用一个或两个下划线

python - 导入错误 : No module named 'twisted'

Python Twisted 守护进程

python - 扭曲的 SSL 套接字连接速度变慢

python - 扭曲地将证书传递给 ssl 处理程序

python - 如何在 Python 中交换元音和辅音

python - 在 pandas/Python 中不间断地计算连续值

python - 'PyDevTerminalInteractiveShell' 对象没有属性 'has_readline'

python - 我是在滥用 Python 的导入系统还是这没问题?

Tiwsted : Twisted requires zope. 接口(interface) 3.6.0 的 Django 错误