title: How to get instance of a class in python
body: <p>I am trying to write some test cases to evaluate the response of a server by creating a client through websockets. I am using autobahn to establish a connection. However, I seem to be unable to send a message to the server because I require the currently active instance of the protocol class in order to run sendMessage. Here is the code:</p>
  
  <pre><code>class SlowSquareClientProtocol(WebSocketClientProtocol):
  
      def onOpen(self):      
          print "Connection established"
  
      def onMessage(self, payload, isBinary):
          if not isBinary:
              res = json.loads(payload.decode('utf8'))
              print("Result received: {}".format(res))
              self.sendClose()
  
      def onClose(self, wasClean, code, reason):
          if reason:
              print(reason)
          reactor.stop()
  
  class NLVRTR(TestFixture,SlowSquareClientProtocol):
      @classmethod
      def setUpClass(self):        
          log.startLogging(sys.stdout)
          factory = WebSocketClientFactory(u"ws://someURL:8078")
          factory.protocol = SlowSquareClientProtocol
          reactor.connectTCP("someURL", 8078, factory)
          wsThread = threading.Thread(target = reactor.run, 
              kwargs={'installSignalHandlers':0})
          wsThread.start()
  
      def test_00_simple(self):
          WSJsonFormatter = WSformat()
          x = WSJsonFormatter.formatGetInfo(2)
          self.sendMessage(json.dumps(x).encode('utf8'))
          print("Request to square {} sent.".format(x))
  </code></pre>
  
  <p>So just to elaborate, I am started the client in the setUpClass method and I am trying to send some messages in test_00_simple. However, I seem to be getting an error like so</p>
  
  <pre><code>AttributeError: 'NLVRTR' object has no attribute 'state'
  </code></pre>
  
  <p>State should be an attribute defined inside the WebSocketClientProtoco. Everything works fine if I put the sendmessage inside the onOpen method, but I cannot call it from anywhere else other than inside SlowSquareClientProtocol class. In the doc for autobahn, it was mentionned that</p>
  
  <pre><code>Whenever a new client connects to the server, a new protocol instance will be created
  </code></pre>
  
  <p>I believe that this is the issue, and that it creates a new protocol instance and the sendmessage method is using that instance. Since I am not calling it inside the slowsquare... class, sendmessage never caught on to this newly created protocol when the client connects thus the error. My question is, is there any way that I can acquire the newly created instance through my code once the client connects?</p>