handlers.py
48 lines
| 1.5 KiB
| text/x-python
|
PythonLexer
Scott Sanderson
|
r18534 | #encoding: utf-8 | ||
Thomas Kluyver
|
r18480 | """Tornado handlers for the terminal emulator.""" | ||
# Copyright (c) IPython Development Team. | ||||
# Distributed under the terms of the Modified BSD License. | ||||
Thomas Kluyver
|
r18494 | import tornado | ||
Thomas Kluyver
|
r18480 | from tornado import web | ||
Thomas Kluyver
|
r18481 | import terminado | ||
Thomas Kluyver
|
r18480 | from ..base.handlers import IPythonHandler | ||
class TerminalHandler(IPythonHandler): | ||||
Thomas Kluyver
|
r18482 | """Render the terminal interface.""" | ||
Thomas Kluyver
|
r18480 | @web.authenticated | ||
Thomas Kluyver
|
r18482 | def get(self, term_name): | ||
self.write(self.render_template('terminal.html', | ||||
ws_path="terminals/websocket/%s" % term_name)) | ||||
Thomas Kluyver
|
r18480 | |||
Thomas Kluyver
|
r18482 | class NewTerminalHandler(IPythonHandler): | ||
"""Redirect to a new terminal.""" | ||||
@web.authenticated | ||||
def get(self): | ||||
name, _ = self.application.terminal_manager.new_named_terminal() | ||||
Thomas Kluyver
|
r18487 | self.redirect(name, permanent=False) | ||
Thomas Kluyver
|
r18480 | |||
Thomas Kluyver
|
r18484 | class TermSocket(terminado.TermSocket, IPythonHandler): | ||
def get(self, *args, **kwargs): | ||||
if not self.get_current_user(): | ||||
raise web.HTTPError(403) | ||||
Thomas Kluyver
|
r18494 | |||
# FIXME: only do super get on tornado ≥ 4 | ||||
# tornado 3 has no get, will raise 405 | ||||
if tornado.version_info >= (4,): | ||||
return super(TermSocket, self).get(*args, **kwargs) | ||||
Thomas Kluyver
|
r18546 | |||
def clear_cookie(self, *args, **kwargs): | ||||
"""meaningless for websockets""" | ||||
pass | ||||
Thomas Kluyver
|
r18494 | |||
def open(self, *args, **kwargs): | ||||
if tornado.version_info < (4,): | ||||
try: | ||||
self.get(*self.open_args, **self.open_kwargs) | ||||
except web.HTTPError: | ||||
self.close() | ||||
raise | ||||
super(TermSocket, self).open(*args, **kwargs) | ||||