hi,
this websocket code works but now i would like to be able to import it to another class to have a way to send messages without waiting for the answer. the code is below here and also pasted here: https://bpaste.net/show/78 36ba3b8008 for some nice syntax highlighting. just running websockets.py works...sending and receiving messages. importing does not, says: builtins.AttributeError: 'MyClientProtocol' object has no attribute 'deferred' any ideas why it does give me this error? but since the code works on its own i dont see why it can be imported. first code paste is the import test which fails, after that is the error mesage and at the end is the code (mywebsocket.py) that is to be imported. from mywebsocket import runIt import sys from autobahn.twisted.websocket import WebSocketClientProtocol, \ WebSocketClientFactory from mywebsocket import MyClientProtocol from twisted.python import log from twisted.internet import reactor if __name__ == '__main__': log.startLogging(sys.stdout) factory = WebSocketClientFactory(u"ws://127.0.0.1:8080/javaee7- websocket-chat/chat/arduino") factory.protocol = MyClientProtocol reactor.connectTCP("127.0.0.1", 8080, factory) d, factory = runIt() def test_sendMessage(result): print("test_sesndMessage():", result) factory.p.sendMessage('python client', 'very nice message') d.addCallback(test_sendMessage) reactor.run() python testimport.py 2017-01-17 12:57:43+0100 [-] Log opened. 2017-01-17 12:57:43+0100 [-] Starting factory <autobahn.twisted.websocket.WebSocketClientFactory object at 0x7fea4c21d358> 2017-01-17 12:57:43+0100 [-] Starting factory <mywebsocket.MyFactory object at 0x7fea48a34b38> 2017-01-17 12:57:43+0100 [-] Server connected: tcp4:127.0.0.1:8080 2017-01-17 12:57:43+0100 [-] WebSocket connection open. 2017-01-17 12:57:43+0100 [-] test_sesndMessage(): yep that worked 2017-01-17 12:57:43+0100 [-] Server connected: tcp4:127.0.0.1:8080 2017-01-17 12:57:43+0100 [-] WebSocket connection open. 2017-01-17 12:57:43+0100 [MyClientProtocol,client] Unhandled Error Traceback (most recent call last): File "/home/julius/code/python/twisted/webchat- client/venv/lib/python3.5/site-packages/twisted/python/log.py", line 103, in callWithLogger return callWithContext({"system": lp}, func, *args, **kw) File "/home/julius/code/python/twisted/webchat- client/venv/lib/python3.5/site-packages/twisted/python/log.py", line 86, in callWithContext return context.call({ILogContext: newCtx}, func, *args, **kw) File "/home/julius/code/python/twisted/webchat- client/venv/lib/python3.5/site-packages/twisted/python/context.py", line 118, in callWithContext return self.currentContext().callWithContext(ctx, func, *args, **kw) File "/home/julius/code/python/twisted/webchat- client/venv/lib/python3.5/site-packages/twisted/python/context.py", line 81, in callWithContext return func(*args,**kw) --- <exception caught here> --- File "/home/julius/code/python/twisted/webchat- client/venv/lib/python3.5/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite why = selectable.doRead() File "/home/julius/code/python/twisted/webchat- client/venv/lib/python3.5/site-packages/twisted/internet/tcp.py", line 208, in doRead return self._dataReceived(data) File "/home/julius/code/python/twisted/webchat- client/venv/lib/python3.5/site-packages/twisted/internet/tcp.py", line 214, in _dataReceived rval = self.protocol.dataReceived(data) File "/home/julius/code/python/twisted/webchat- client/venv/lib/python3.5/site-packages/autobahn/twisted/websocket.py", line 132, in dataReceived self._dataReceived(data) File "/home/julius/code/python/twisted/webchat- client/venv/lib/python3.5/site- packages/autobahn/websocket/protocol.py", line 1183, in _dataReceived self.consumeData() File "/home/julius/code/python/twisted/webchat- client/venv/lib/python3.5/site- packages/autobahn/websocket/protocol.py", line 1212, in consumeData self.processHandshake() File "/home/julius/code/python/twisted/webchat- client/venv/lib/python3.5/site- packages/autobahn/websocket/protocol.py", line 3784, in processHandshake self._onOpen() File "/home/julius/code/python/twisted/webchat- client/venv/lib/python3.5/site-packages/autobahn/twisted/websocket.py", line 142, in _onOpen self.onOpen() File "/home/julius/code/python/twisted/webchat- client/mywebsocket.py", line 18, in onOpen self.deferred.callback('yep that worked') builtins.AttributeError: 'MyClientProtocol' object has no attribute 'deferred' 2017-01-17 12:57:43+0100 [-] WebSocket connection closed: connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake) 2017-01-17 12:57:43+0100 [-] Stopping factory <autobahn.twisted.websocket.WebSocketClientFactory object at 0x7fea4c21d358> 2017-01-17 12:57:43+0100 [-] Text message received: {"message":"very nice message","sender":"python client","received":"Tue Jan 17 12:57:43 CET 2017"} ^C2017-01-17 12:57:44+0100 [-] Received SIGINT, shutting down. 2017-01-17 12:57:44+0100 [-] WebSocket connection closed: connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake) 2017-01-17 12:57:44+0100 [-] Stopping factory <mywebsocket.MyFactory object at 0x7fea48a34b38> 2017-01-17 12:57:44+0100 [-] Main loop terminated. mywebsocket.py from autobahn.twisted.websocket import WebSocketClientProtocol, \ WebSocketClientFactory import sys from twisted.python import log from twisted.internet import reactor, defer import json class MyClientProtocol(WebSocketClientProtocol): def onConnect(self, response): tmp = "Server connected: {0}".format(response.peer) print(tmp) def onOpen(self): print("WebSocket connection open.") self.deferred.callback('yep that worked') def hello(): print("sending message") message = json.dumps({"message":"1" ,"sender":"2", "received":"3"}).encode('utf-8') self.sendMessage(message) self.factory.reactor.callLater(10, hello) def sendMessage(self, sender, message): message = json.dumps({"message": message, "sender": sender, "received": "notusedyet"}) super(MyClientProtocol, self).sendMessage(message.encode('utf- 8')) def onMessage(self, payload, isBinary): if isBinary: print("Binary message received: {0} bytes".format(len(payload))) else: print("Text message received: {0}".format(payload.decode('utf8'))) def onClose(self, wasClean, code, reason): print("WebSocket connection closed: {0}".format(reason)) class MyFactory(WebSocketClientFactory): def __init__(self, deferred, *args, **kwargs): self.d = deferred super(MyFactory, self).__init__(*args, **kwargs) def buildProtocol(self, addr): p = super(MyFactory, self).buildProtocol(addr) self.p = p p.deferred = self.d return p def runIt(): log.startLogging(sys.stdout) d = defer.Deferred() factory = MyFactory(d, u"ws://127.0.0.1:8080/javaee7-websocket- chat/chat/arduino") factory.protocol = MyClientProtocol def test_sendMessage(result): print("test_sesndMessage():", result) factory.p.sendMessage('python client', 'very nice message') reactor.connectTCP("127.0.0.1", 8080, factory) return (d, factory) if __name__ == '__main__': runIt() reactor.run() _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python