ioloop.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. """ZAP Authenticator integrated with the tornado IOLoop.
  2. .. versionadded:: 14.1
  3. """
  4. # Copyright (C) PyZMQ Developers
  5. # Distributed under the terms of the Modified BSD License.
  6. from tornado import ioloop
  7. from zmq.eventloop import zmqstream
  8. from .base import Authenticator
  9. class IOLoopAuthenticator(Authenticator):
  10. """ZAP authentication for use in the tornado IOLoop"""
  11. def __init__(self, context=None, encoding='utf-8', log=None, io_loop=None):
  12. super(IOLoopAuthenticator, self).__init__(context, encoding, log)
  13. self.zap_stream = None
  14. self.io_loop = io_loop or ioloop.IOLoop.current()
  15. def start(self):
  16. """Start ZAP authentication"""
  17. super(IOLoopAuthenticator, self).start()
  18. self.zap_stream = zmqstream.ZMQStream(self.zap_socket, self.io_loop)
  19. self.zap_stream.on_recv(self.handle_zap_message)
  20. def stop(self):
  21. """Stop ZAP authentication"""
  22. if self.zap_stream:
  23. self.zap_stream.close()
  24. self.zap_stream = None
  25. super(IOLoopAuthenticator, self).stop()
  26. __all__ = ['IOLoopAuthenticator']