(Twisted-Autobahn) exceptions.AttributeError: 'WsProtocol' object has no attribute 'factory'
(Twisted-Autobahn) exceptions.AttributeError: 'WsProtocol' object has no attribute 'factory'
目前如标题所示,我收到以下错误。我使用 Autobahn 的扭曲库的 Web Socket 代码如下。当我实例化 WsProtocolFactory
时,我想将协议设置为 WsProtocol
并使用 defaultdict
初始化工厂以订阅主题。
我最初的想法是在 WsProtocol
__init_
中添加一个 factory
参数,但未能修复错误。
如有任何帮助,我们将不胜感激。
谢谢!
编辑:添加回溯错误
Unhandled Error
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
why = selectable.doRead()
--- <exception caught here> ---
File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 1074, in doRead
protocol.makeConnection(transport)
File "/usr/lib64/python2.7/site-packages/twisted/internet/protocol.py", line 494, in makeConnection
self.connectionMade()
File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 95, in connectionMade
self._connectionMade()
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 2426, in _connectionMade
WebSocketProtocol._connectionMade(self)
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 962, in _connectionMade
setattr(self, configAttr, getattr(self.factory, configAttr))
exceptions.AttributeError: 'WsProtocol' object has no attribute 'factory'
Unhandled Error
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
why = selectable.doRead()
File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 209, in doRead
return self._dataReceived(data)
File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 215, in _dataReceived
rval = self.protocol.dataReceived(data)
File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 132, in dataReceived
self._dataReceived(data)
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 1175, in _dataReceived
if self.state == WebSocketProtocol.STATE_OPEN:
exceptions.AttributeError: 'WsProtocol' object has no attribute 'state'
Unhandled Error
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/twisted/internet/task.py", line 936, in react
_reactor.run()
File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1194, in run
self.mainLoop()
File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1206, in mainLoop
self.doIteration(t)
File "/usr/lib64/python2.7/site-packages/twisted/internet/epollreactor.py", line 396, in doPoll
log.callWithLogger(selectable, _drdw, selectable, fd, event)
--- <exception caught here> ---
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 610, in _doReadOrWrite
self._disconnectSelectable(selectable, why, inRead)
File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 258, in _disconnectSelectable
selectable.connectionLost(failure.Failure(why))
File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 293, in connectionLost
protocol.connectionLost(reason)
File "web_autobahn.py", line 77, in connectionLost
WebSocketServerProtocol.connectionLost(self, reason)
File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 129, in connectionLost
self._connectionLost(reason)
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 2437, in _connectionLost
WebSocketProtocol._connectionLost(self, reason)
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 1075, in _connectionLost
if not self.factory.isServer and self.serverConnectionDropTimeoutCall is not None:
exceptions.AttributeError: 'WsProtocol' object has no attribute 'factory'
这是我的代码
from datetime import datetime
from collections import defaultdict
from twisted.web import resource, server
from twisted.internet.task import react
from twisted.internet import reactor
from twisted.internet.defer import DeferredList, Deferred
from twisted.internet.endpoints import TCP4ServerEndpoint
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
from schema import schema
import json
import os
import sys
from twisted.python import log
def log(msg):
print("{}: {}".format(str(datetime.now()), msg))
class WsProtocol(WebSocketServerProtocol):
"""Dummy websocket protocol"""
def __init__(self, topics):
super(WsProtocol, self).__init__()
self.topics = topics
self.subscribed_topic = None
log("WS protocol init")
def onConnect(self, request):
print("WebSocket connected.")
def onOpen(self):
print("WebSocket connection open.")
def onMessage(self, payload, isBinary):
log(payload)
self.sendMessage("Message received")
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {}".format(reason))
def connectionLost(self, reason):
WebSocketServerProtocol.connectionLost(self, reason)
log("Connection closed: Reason is {}".format(reason))
class WsProtocolFactory(WebSocketServerFactory):
def __init__(self):
self.topics = defaultdict(set)
def buildProtocol(self, *args, **kwargs):
return WsProtocol(self.topics)
def setup_protocols(reactor):
factories = {
WsProtocolFactory: 9001,
}
for factory, port in factories.items():
endpoint = TCP4ServerEndpoint(reactor, port)
endpoint.listen(factory())
log("Bound port {} to protocol {}".format(str(port), factory))
return Deferred()
if __name__ == '__main__':
react(setup_protocols)
编辑:新回溯
Unhandled Error
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
why = selectable.doRead()
--- <exception caught here> ---
File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 1074, in doRead
protocol.makeConnection(transport)
File "/usr/lib64/python2.7/site-packages/twisted/internet/protocol.py", line 494, in makeConnection
self.connectionMade()
File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 95, in connectionMade
self._connectionMade()
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 2426, in _connectionMade
WebSocketProtocol._connectionMade(self)
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 962, in _connectionMade
setattr(self, configAttr, getattr(self.factory, configAttr))
exceptions.AttributeError: 'WsProtocolFactory' object has no attribute 'logOctets'
Unhandled Error
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
why = selectable.doRead()
File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 209, in doRead
return self._dataReceived(data)
File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 215, in _dataReceived
rval = self.protocol.dataReceived(data)
File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 132, in dataReceived
self._dataReceived(data)
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 1175, in _dataReceived
if self.state == WebSocketProtocol.STATE_OPEN:
exceptions.AttributeError: 'WsProtocol' object has no attribute 'state'
Unhandled Error
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/twisted/internet/task.py", line 936, in react
_reactor.run()
File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1194, in run
self.mainLoop()
File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1206, in mainLoop
self.doIteration(t)
File "/usr/lib64/python2.7/site-packages/twisted/internet/epollreactor.py", line 396, in doPoll
log.callWithLogger(selectable, _drdw, selectable, fd, event)
--- <exception caught here> ---
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 610, in _doReadOrWrite
self._disconnectSelectable(selectable, why, inRead)
File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 258, in _disconnectSelectable
selectable.connectionLost(failure.Failure(why))
File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 293, in connectionLost
protocol.connectionLost(reason)
File "web_autobahn.py", line 77, in connectionLost
WebSocketServerProtocol.connectionLost(self, reason)
File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 129, in connectionLost
self._connectionLost(reason)
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 2437, in _connectionLost
WebSocketProtocol._connectionLost(self, reason)
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 1082, in _connectionLost
if self.autoPingPendingCall:
exceptions.AttributeError: 'WsProtocol' object has no attribute 'autoPingPendingCall'
编辑:正确的代码有效
from datetime import datetime
from collections import defaultdict
from twisted.web import resource, server
from twisted.internet.task import react
from twisted.internet import reactor
from twisted.internet.defer import DeferredList, Deferred
from twisted.internet.endpoints import TCP4ServerEndpoint
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
from schema import schema
import json
import os
import sys
from twisted.python import log
def log(msg):
print("{}: {}".format(str(datetime.now()), msg))
class WsProtocol(WebSocketServerProtocol):
"""Dummy websocket protocol"""
def __init__(self, topics):
super(WsProtocol, self).__init__()
self.topics = topics
self.subscribed_topic = None
log("WS protocol init")
def onConnect(self, request):
print("WebSocket connected.")
def onOpen(self):
print("WebSocket connection open.")
def onMessage(self, payload, isBinary):
log(payload)
self.sendMessage("Message received")
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {}".format(reason))
def connectionLost(self, reason):
WebSocketServerProtocol.connectionLost(self, reason)
log("Connection closed: Reason is {}".format(reason))
class WsProtocolFactory(WebSocketServerFactory):
def __init__(self):
super(WsProtocolFactory, self).__init__()
self.topics = defaultdict(set)
def buildProtocol(self, *args, **kwargs):
protocol = WsProtocol(self.topics)
protocol.factory = self
return protocol
def setup_protocols(reactor):
factories = {
WsProtocolFactory: 9001,
}
for factory, port in factories.items():
endpoint = TCP4ServerEndpoint(reactor, port)
endpoint.listen(factory())
log("Bound port {} to protocol {}".format(str(port), factory))
return Deferred()
if __name__ == '__main__':
react(setup_protocols)
定义工厂的 buildProtocol
以便它为构建的协议提供 factory
属性:
def buildProtocol(self, *args, **kwargs):
protocol = WsProtocol(self.topics)
protocol.factory = self
return protocol
以这种方式设置工厂是许多不同的使用 Twisted 的代码所采用的普遍惯例。
我不确定 logOctets
是什么,但我猜这是由类似问题引起的。
我们再来看看buildProtocol
。您的 class 覆盖了 buildProtocol
的基本实现,它执行如下操作:
def buildProtocol(self, *args, **kwargs):
protocol = self.protocol()
protocol.factory = self
return protocol
您的版本由于没有重新实现所有这些行为而导致了第一个 AttributeError(对于 factory
),解决方法是更新您的版本,使其更接近原始版本。
我的猜测是 WebSocketServerFactory.__init__
有一些额外的行为,而您的子 class 的 __init__
破坏了它。解决方法是复制它或(更好)调用基本实现。
目前如标题所示,我收到以下错误。我使用 Autobahn 的扭曲库的 Web Socket 代码如下。当我实例化 WsProtocolFactory
时,我想将协议设置为 WsProtocol
并使用 defaultdict
初始化工厂以订阅主题。
我最初的想法是在 WsProtocol
__init_
中添加一个 factory
参数,但未能修复错误。
如有任何帮助,我们将不胜感激。
谢谢!
编辑:添加回溯错误
Unhandled Error
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
why = selectable.doRead()
--- <exception caught here> ---
File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 1074, in doRead
protocol.makeConnection(transport)
File "/usr/lib64/python2.7/site-packages/twisted/internet/protocol.py", line 494, in makeConnection
self.connectionMade()
File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 95, in connectionMade
self._connectionMade()
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 2426, in _connectionMade
WebSocketProtocol._connectionMade(self)
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 962, in _connectionMade
setattr(self, configAttr, getattr(self.factory, configAttr))
exceptions.AttributeError: 'WsProtocol' object has no attribute 'factory'
Unhandled Error
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
why = selectable.doRead()
File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 209, in doRead
return self._dataReceived(data)
File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 215, in _dataReceived
rval = self.protocol.dataReceived(data)
File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 132, in dataReceived
self._dataReceived(data)
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 1175, in _dataReceived
if self.state == WebSocketProtocol.STATE_OPEN:
exceptions.AttributeError: 'WsProtocol' object has no attribute 'state'
Unhandled Error
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/twisted/internet/task.py", line 936, in react
_reactor.run()
File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1194, in run
self.mainLoop()
File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1206, in mainLoop
self.doIteration(t)
File "/usr/lib64/python2.7/site-packages/twisted/internet/epollreactor.py", line 396, in doPoll
log.callWithLogger(selectable, _drdw, selectable, fd, event)
--- <exception caught here> ---
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 610, in _doReadOrWrite
self._disconnectSelectable(selectable, why, inRead)
File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 258, in _disconnectSelectable
selectable.connectionLost(failure.Failure(why))
File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 293, in connectionLost
protocol.connectionLost(reason)
File "web_autobahn.py", line 77, in connectionLost
WebSocketServerProtocol.connectionLost(self, reason)
File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 129, in connectionLost
self._connectionLost(reason)
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 2437, in _connectionLost
WebSocketProtocol._connectionLost(self, reason)
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 1075, in _connectionLost
if not self.factory.isServer and self.serverConnectionDropTimeoutCall is not None:
exceptions.AttributeError: 'WsProtocol' object has no attribute 'factory'
这是我的代码
from datetime import datetime
from collections import defaultdict
from twisted.web import resource, server
from twisted.internet.task import react
from twisted.internet import reactor
from twisted.internet.defer import DeferredList, Deferred
from twisted.internet.endpoints import TCP4ServerEndpoint
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
from schema import schema
import json
import os
import sys
from twisted.python import log
def log(msg):
print("{}: {}".format(str(datetime.now()), msg))
class WsProtocol(WebSocketServerProtocol):
"""Dummy websocket protocol"""
def __init__(self, topics):
super(WsProtocol, self).__init__()
self.topics = topics
self.subscribed_topic = None
log("WS protocol init")
def onConnect(self, request):
print("WebSocket connected.")
def onOpen(self):
print("WebSocket connection open.")
def onMessage(self, payload, isBinary):
log(payload)
self.sendMessage("Message received")
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {}".format(reason))
def connectionLost(self, reason):
WebSocketServerProtocol.connectionLost(self, reason)
log("Connection closed: Reason is {}".format(reason))
class WsProtocolFactory(WebSocketServerFactory):
def __init__(self):
self.topics = defaultdict(set)
def buildProtocol(self, *args, **kwargs):
return WsProtocol(self.topics)
def setup_protocols(reactor):
factories = {
WsProtocolFactory: 9001,
}
for factory, port in factories.items():
endpoint = TCP4ServerEndpoint(reactor, port)
endpoint.listen(factory())
log("Bound port {} to protocol {}".format(str(port), factory))
return Deferred()
if __name__ == '__main__':
react(setup_protocols)
编辑:新回溯
Unhandled Error
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
why = selectable.doRead()
--- <exception caught here> ---
File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 1074, in doRead
protocol.makeConnection(transport)
File "/usr/lib64/python2.7/site-packages/twisted/internet/protocol.py", line 494, in makeConnection
self.connectionMade()
File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 95, in connectionMade
self._connectionMade()
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 2426, in _connectionMade
WebSocketProtocol._connectionMade(self)
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 962, in _connectionMade
setattr(self, configAttr, getattr(self.factory, configAttr))
exceptions.AttributeError: 'WsProtocolFactory' object has no attribute 'logOctets'
Unhandled Error
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
why = selectable.doRead()
File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 209, in doRead
return self._dataReceived(data)
File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 215, in _dataReceived
rval = self.protocol.dataReceived(data)
File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 132, in dataReceived
self._dataReceived(data)
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 1175, in _dataReceived
if self.state == WebSocketProtocol.STATE_OPEN:
exceptions.AttributeError: 'WsProtocol' object has no attribute 'state'
Unhandled Error
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/twisted/internet/task.py", line 936, in react
_reactor.run()
File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1194, in run
self.mainLoop()
File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1206, in mainLoop
self.doIteration(t)
File "/usr/lib64/python2.7/site-packages/twisted/internet/epollreactor.py", line 396, in doPoll
log.callWithLogger(selectable, _drdw, selectable, fd, event)
--- <exception caught here> ---
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 610, in _doReadOrWrite
self._disconnectSelectable(selectable, why, inRead)
File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 258, in _disconnectSelectable
selectable.connectionLost(failure.Failure(why))
File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 293, in connectionLost
protocol.connectionLost(reason)
File "web_autobahn.py", line 77, in connectionLost
WebSocketServerProtocol.connectionLost(self, reason)
File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 129, in connectionLost
self._connectionLost(reason)
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 2437, in _connectionLost
WebSocketProtocol._connectionLost(self, reason)
File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 1082, in _connectionLost
if self.autoPingPendingCall:
exceptions.AttributeError: 'WsProtocol' object has no attribute 'autoPingPendingCall'
编辑:正确的代码有效
from datetime import datetime
from collections import defaultdict
from twisted.web import resource, server
from twisted.internet.task import react
from twisted.internet import reactor
from twisted.internet.defer import DeferredList, Deferred
from twisted.internet.endpoints import TCP4ServerEndpoint
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
from schema import schema
import json
import os
import sys
from twisted.python import log
def log(msg):
print("{}: {}".format(str(datetime.now()), msg))
class WsProtocol(WebSocketServerProtocol):
"""Dummy websocket protocol"""
def __init__(self, topics):
super(WsProtocol, self).__init__()
self.topics = topics
self.subscribed_topic = None
log("WS protocol init")
def onConnect(self, request):
print("WebSocket connected.")
def onOpen(self):
print("WebSocket connection open.")
def onMessage(self, payload, isBinary):
log(payload)
self.sendMessage("Message received")
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {}".format(reason))
def connectionLost(self, reason):
WebSocketServerProtocol.connectionLost(self, reason)
log("Connection closed: Reason is {}".format(reason))
class WsProtocolFactory(WebSocketServerFactory):
def __init__(self):
super(WsProtocolFactory, self).__init__()
self.topics = defaultdict(set)
def buildProtocol(self, *args, **kwargs):
protocol = WsProtocol(self.topics)
protocol.factory = self
return protocol
def setup_protocols(reactor):
factories = {
WsProtocolFactory: 9001,
}
for factory, port in factories.items():
endpoint = TCP4ServerEndpoint(reactor, port)
endpoint.listen(factory())
log("Bound port {} to protocol {}".format(str(port), factory))
return Deferred()
if __name__ == '__main__':
react(setup_protocols)
定义工厂的 buildProtocol
以便它为构建的协议提供 factory
属性:
def buildProtocol(self, *args, **kwargs):
protocol = WsProtocol(self.topics)
protocol.factory = self
return protocol
以这种方式设置工厂是许多不同的使用 Twisted 的代码所采用的普遍惯例。
我不确定 logOctets
是什么,但我猜这是由类似问题引起的。
我们再来看看buildProtocol
。您的 class 覆盖了 buildProtocol
的基本实现,它执行如下操作:
def buildProtocol(self, *args, **kwargs):
protocol = self.protocol()
protocol.factory = self
return protocol
您的版本由于没有重新实现所有这些行为而导致了第一个 AttributeError(对于 factory
),解决方法是更新您的版本,使其更接近原始版本。
我的猜测是 WebSocketServerFactory.__init__
有一些额外的行为,而您的子 class 的 __init__
破坏了它。解决方法是复制它或(更好)调用基本实现。