handlers.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #encoding: utf-8
  2. """Tornado handlers for the terminal emulator."""
  3. # Copyright (c) Jupyter Development Team.
  4. # Distributed under the terms of the Modified BSD License.
  5. from tornado import web
  6. import terminado
  7. from notebook._tz import utcnow
  8. from ..base.handlers import IPythonHandler
  9. from ..base.zmqhandlers import WebSocketMixin
  10. class TerminalHandler(IPythonHandler):
  11. """Render the terminal interface."""
  12. @web.authenticated
  13. def get(self, term_name):
  14. self.write(self.render_template('terminal.html',
  15. ws_path="terminals/websocket/%s" % term_name))
  16. class TermSocket(WebSocketMixin, IPythonHandler, terminado.TermSocket):
  17. def origin_check(self):
  18. """Terminado adds redundant origin_check
  19. Tornado already calls check_origin, so don't do anything here.
  20. """
  21. return True
  22. def get(self, *args, **kwargs):
  23. if not self.get_current_user():
  24. raise web.HTTPError(403)
  25. return super(TermSocket, self).get(*args, **kwargs)
  26. def on_message(self, message):
  27. super(TermSocket, self).on_message(message)
  28. self.application.settings['terminal_last_activity'] = utcnow()
  29. def write_message(self, message, binary=False):
  30. super(TermSocket, self).write_message(message, binary=binary)
  31. self.application.settings['terminal_last_activity'] = utcnow()