##// END OF EJS Templates
remove mktemp usage...
remove mktemp usage mktemp is insecure, by the time the generated filename is used someone else might have modified it.

File last commit:

r15372:5ce8451d
r15372:5ce8451d
Show More
connect.py
560 lines | 18.3 KiB | text/x-python | PythonLexer
MinRK
split kernel.util into kernel.connect and kernel.launcher
r9355 """Utilities for connecting to kernels
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351
Authors:
* Min Ragan-Kelley
"""
#-----------------------------------------------------------------------------
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352 # Copyright (C) 2013 The IPython Development Team
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351 #
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
MinRK
move connect_[channel] to ConnectionFileMixin
r10324 from __future__ import absolute_import
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351 import glob
import json
import os
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352 import socket
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351 import sys
from getpass import getpass
from subprocess import Popen, PIPE
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352 import tempfile
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351
MinRK
move connect_[channel] to ConnectionFileMixin
r10324 import zmq
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351 # external imports
from IPython.external.ssh import tunnel
# IPython imports
MinRK
ConnectionFileMixin is Configurable...
r12353 from IPython.config import Configurable
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351 from IPython.core.profiledir import ProfileDir
MinRK
avoid executing code in utils.localinterfaces at import time...
r12591 from IPython.utils.localinterfaces import localhost
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351 from IPython.utils.path import filefind, get_ipython_dir
Thomas Kluyver
Replace references to unicode and basestring
r13353 from IPython.utils.py3compat import (str_to_bytes, bytes_to_str, cast_bytes_py2,
string_types)
MinRK
split KernelManager into KernelManager + KernelClient
r10285 from IPython.utils.traitlets import (
Bool, Integer, Unicode, CaselessStrEnum,
)
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351
#-----------------------------------------------------------------------------
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352 # Working with Connection Files
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351 #-----------------------------------------------------------------------------
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352 def write_connection_file(fname=None, shell_port=0, iopub_port=0, stdin_port=0, hb_port=0,
MinRK
avoid executing code in utils.localinterfaces at import time...
r12591 control_port=0, ip='', key=b'', transport='tcp',
MinRK
add signature_scheme to Session and connection files...
r11656 signature_scheme='hmac-sha256',
):
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352 """Generates a JSON config file, including the selection of random ports.
Parameters
----------
fname : unicode
The path to the file to write
shell_port : int, optional
MinRK
fix typo in write_connection_file
r10328 The port to use for ROUTER (shell) channel.
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352
iopub_port : int, optional
The port to use for the SUB channel.
stdin_port : int, optional
MinRK
add control channel...
r10296 The port to use for the ROUTER (raw input) channel.
control_port : int, optional
MinRK
fix typo in write_connection_file
r10328 The port to use for the ROUTER (control) channel.
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352
hb_port : int, optional
MinRK
fix typo in write_connection_file
r10328 The port to use for the heartbeat REP channel.
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352
ip : str, optional
The ip address the kernel will bind to.
key : str, optional
MinRK
add signature_scheme to Session and connection files...
r11656 The Session key used for message authentication.
signature_scheme : str, optional
The scheme used for message authentication.
This has the form 'digest-hash', where 'digest'
is the scheme used for digests, and 'hash' is the name of the hash function
used by the digest scheme.
Currently, 'hmac' is the only supported digest scheme,
and 'sha256' is the default hash function.
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352
"""
MinRK
avoid executing code in utils.localinterfaces at import time...
r12591 if not ip:
ip = localhost()
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352 # default to temporary connector file
if not fname:
Julian Taylor
remove mktemp usage...
r15372 fd, fname = tempfile.mkstemp('.json')
os.close(fd)
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352
# Find open ports as necessary.
ports = []
MinRK
add control channel...
r10296 ports_needed = int(shell_port <= 0) + \
int(iopub_port <= 0) + \
int(stdin_port <= 0) + \
int(control_port <= 0) + \
int(hb_port <= 0)
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352 if transport == 'tcp':
for i in range(ports_needed):
sock = socket.socket()
sock.bind(('', 0))
ports.append(sock)
for i, sock in enumerate(ports):
port = sock.getsockname()[1]
sock.close()
ports[i] = port
else:
N = 1
for i in range(ports_needed):
while os.path.exists("%s-%s" % (ip, str(N))):
N += 1
ports.append(N)
N += 1
if shell_port <= 0:
shell_port = ports.pop(0)
if iopub_port <= 0:
iopub_port = ports.pop(0)
if stdin_port <= 0:
stdin_port = ports.pop(0)
MinRK
add control channel...
r10296 if control_port <= 0:
control_port = ports.pop(0)
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352 if hb_port <= 0:
hb_port = ports.pop(0)
cfg = dict( shell_port=shell_port,
iopub_port=iopub_port,
stdin_port=stdin_port,
MinRK
add control channel...
r10296 control_port=control_port,
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352 hb_port=hb_port,
)
cfg['ip'] = ip
cfg['key'] = bytes_to_str(key)
cfg['transport'] = transport
MinRK
add signature_scheme to Session and connection files...
r11656 cfg['signature_scheme'] = signature_scheme
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352
with open(fname, 'w') as f:
f.write(json.dumps(cfg, indent=2))
return fname, cfg
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351 def get_connection_file(app=None):
"""Return the path to the connection file of an app
Parameters
----------
MinRK
fix a few remaining KernelApp/IPKernelApp changes
r9516 app : IPKernelApp instance [optional]
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351 If unspecified, the currently running app will be used
"""
if app is None:
MinRK
mv IPython.zmq to IPython.kernel.zmq
r9372 from IPython.kernel.zmq.kernelapp import IPKernelApp
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351 if not IPKernelApp.initialized():
raise RuntimeError("app not specified, and not in a running Kernel")
app = IPKernelApp.instance()
return filefind(app.connection_file, ['.', app.profile_dir.security_dir])
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351 def find_connection_file(filename, profile=None):
"""find a connection file, and return its absolute path.
The current working directory and the profile's security
directory will be searched for the file if it is not given by
absolute path.
If profile is unspecified, then the current running application's
profile will be used, or 'default', if not run from IPython.
If the argument does not match an existing file, it will be interpreted as a
fileglob, and the matching file in the profile's security dir with
the latest access time will be used.
Parameters
----------
filename : str
The connection file or fileglob to search for.
profile : str [optional]
The name of the profile to use when searching for the connection file,
if different from the current IPython session or 'default'.
Returns
-------
str : The absolute path of the connection file.
"""
from IPython.core.application import BaseIPythonApplication as IPApp
try:
# quick check for absolute path, before going through logic
return filefind(filename)
except IOError:
pass
if profile is None:
# profile unspecified, check if running from an IPython app
if IPApp.initialized():
app = IPApp.instance()
profile_dir = app.profile_dir
else:
# not running in IPython, use default profile
profile_dir = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), 'default')
else:
# find profiledir by profile name:
profile_dir = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
security_dir = profile_dir.security_dir
try:
# first, try explicit name
return filefind(filename, ['.', security_dir])
except IOError:
pass
# not found by full name
if '*' in filename:
# given as a glob already
pat = filename
else:
# accept any substring match
pat = '*%s*' % filename
matches = glob.glob( os.path.join(security_dir, pat) )
if not matches:
raise IOError("Could not find %r in %r" % (filename, security_dir))
elif len(matches) == 1:
return matches[0]
else:
# get most recent match, by access time:
return sorted(matches, key=lambda f: os.stat(f).st_atime)[-1]
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351 def get_connection_info(connection_file=None, unpack=False, profile=None):
"""Return the connection information for the current Kernel.
Parameters
----------
connection_file : str [optional]
The connection file to be used. Can be given by absolute path, or
IPython will search in the security directory of a given profile.
If run from IPython,
If unspecified, the connection file for the currently running
IPython Kernel will be used, which is only allowed from inside a kernel.
unpack : bool [default: False]
if True, return the unpacked dict, otherwise just the string contents
of the file.
profile : str [optional]
The name of the profile to use when searching for the connection file,
if different from the current IPython session or 'default'.
Returns
-------
The connection dictionary of the current kernel, as string or dict,
depending on `unpack`.
"""
if connection_file is None:
# get connection file from current kernel
cf = get_connection_file()
else:
# connection file specified, allow shortnames:
cf = find_connection_file(connection_file, profile=profile)
with open(cf) as f:
info = f.read()
if unpack:
info = json.loads(info)
# ensure key is bytes:
info['key'] = str_to_bytes(info.get('key', ''))
return info
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351 def connect_qtconsole(connection_file=None, argv=None, profile=None):
"""Connect a qtconsole to the current kernel.
This is useful for connecting a second qtconsole to a kernel, or to a
local notebook.
Parameters
----------
connection_file : str [optional]
The connection file to be used. Can be given by absolute path, or
IPython will search in the security directory of a given profile.
If run from IPython,
If unspecified, the connection file for the currently running
IPython Kernel will be used, which is only allowed from inside a kernel.
argv : list [optional]
Any extra args to be passed to the console.
profile : str [optional]
The name of the profile to use when searching for the connection file,
if different from the current IPython session or 'default'.
Returns
-------
subprocess.Popen instance running the qtconsole frontend
"""
argv = [] if argv is None else argv
if connection_file is None:
# get connection file from current kernel
cf = get_connection_file()
else:
cf = find_connection_file(connection_file, profile=profile)
cmd = ';'.join([
Fernando Perez
Fix all remaining imports that used `IPython.frontend`.
r11024 "from IPython.qt.console import qtconsoleapp",
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351 "qtconsoleapp.main()"
])
MinRK
close fds when spawning qtconsole subprocess...
r10336 return Popen([sys.executable, '-c', cmd, '--existing', cf] + argv,
MinRK
don't close_fds on Windows...
r12180 stdout=PIPE, stderr=PIPE, close_fds=(sys.platform != 'win32'),
MinRK
close fds when spawning qtconsole subprocess...
r10336 )
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351 def tunnel_to_kernel(connection_info, sshserver, sshkey=None):
"""tunnel connections to a kernel via ssh
This will open four SSH tunnels from localhost on this machine to the
ports associated with the kernel. They can be either direct
localhost-localhost tunnels, or if an intermediate server is necessary,
the kernel must be listening on a public IP.
Parameters
----------
connection_info : dict or str (path)
Either a connection dict, or the path to a JSON connection file
sshserver : str
The ssh sever to use to tunnel to the kernel. Can be a full
`user@server:port` string. ssh config aliases are respected.
sshkey : str [optional]
Path to file containing ssh key to use for authentication.
Only necessary if your ssh config does not already associate
a keyfile with the host.
Returns
-------
(shell, iopub, stdin, hb) : ints
The four ports on localhost that have been forwarded to the kernel.
"""
Thomas Kluyver
Replace references to unicode and basestring
r13353 if isinstance(connection_info, string_types):
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351 # it's a path, unpack it
with open(connection_info) as f:
connection_info = json.loads(f.read())
cf = connection_info
lports = tunnel.select_random_ports(4)
rports = cf['shell_port'], cf['iopub_port'], cf['stdin_port'], cf['hb_port']
remote_ip = cf['ip']
if tunnel.try_passwordless_ssh(sshserver, sshkey):
password=False
else:
Thomas Kluyver
Use cast_bytes_py2 for getpass() prompt
r12893 password = getpass("SSH Password for %s: " % cast_bytes_py2(sshserver))
MinRK
move IPython.lib.kernel to IPython.utils.kernel...
r9351
for lp,rp in zip(lports, rports):
tunnel.ssh_tunnel(lp, rp, sshserver, remote_ip, sshkey, password)
return tuple(lports)
MinRK
move IPython.zmq.entry_point to IPython.utils.kernel
r9352
MinRK
split KernelManager into KernelManager + KernelClient
r10285
#-----------------------------------------------------------------------------
MinRK
move connect_[channel] to ConnectionFileMixin
r10324 # Mixin for classes that work with connection files
MinRK
split KernelManager into KernelManager + KernelClient
r10285 #-----------------------------------------------------------------------------
MinRK
move connect_[channel] to ConnectionFileMixin
r10324
channel_socket_types = {
'hb' : zmq.REQ,
'shell' : zmq.DEALER,
'iopub' : zmq.SUB,
'stdin' : zmq.DEALER,
'control': zmq.DEALER,
}
MinRK
add control channel...
r10296 port_names = [ "%s_port" % channel for channel in ('shell', 'stdin', 'iopub', 'hb', 'control')]
MinRK
split KernelManager into KernelManager + KernelClient
r10285
MinRK
ConnectionFileMixin is Configurable...
r12353 class ConnectionFileMixin(Configurable):
MinRK
split KernelManager into KernelManager + KernelClient
r10285 """Mixin for configurable classes that work with connection files"""
# The addresses for the communication channels
connection_file = Unicode('')
_connection_file_written = Bool(False)
transport = CaselessStrEnum(['tcp', 'ipc'], default_value='tcp', config=True)
MinRK
avoid executing code in utils.localinterfaces at import time...
r12591 ip = Unicode(config=True,
MinRK
split KernelManager into KernelManager + KernelClient
r10285 help="""Set the kernel\'s IP address [default localhost].
If the IP address is something other than localhost, then
Consoles on other machines will be able to connect
to the Kernel, so be careful!"""
)
def _ip_default(self):
if self.transport == 'ipc':
if self.connection_file:
return os.path.splitext(self.connection_file)[0] + '-ipc'
else:
return 'kernel-ipc'
else:
MinRK
avoid executing code in utils.localinterfaces at import time...
r12591 return localhost()
MinRK
split KernelManager into KernelManager + KernelClient
r10285
def _ip_changed(self, name, old, new):
if new == '*':
self.ip = '0.0.0.0'
# protected traits
shell_port = Integer(0)
iopub_port = Integer(0)
stdin_port = Integer(0)
MinRK
add control channel...
r10296 control_port = Integer(0)
MinRK
split KernelManager into KernelManager + KernelClient
r10285 hb_port = Integer(0)
MinRK
add control channel...
r10296 @property
def ports(self):
return [ getattr(self, name) for name in port_names ]
MinRK
split KernelManager into KernelManager + KernelClient
r10285 #--------------------------------------------------------------------------
# Connection and ipc file management
#--------------------------------------------------------------------------
MinRK
add control channel...
r10296 def get_connection_info(self):
"""return the connection info as a dict"""
return dict(
transport=self.transport,
ip=self.ip,
shell_port=self.shell_port,
iopub_port=self.iopub_port,
stdin_port=self.stdin_port,
hb_port=self.hb_port,
control_port=self.control_port,
MinRK
test KernelManager.get_connection_info
r11840 signature_scheme=self.session.signature_scheme,
MinRK
signature_scheme lives in Session...
r11816 key=self.session.key,
MinRK
add control channel...
r10296 )
MinRK
split KernelManager into KernelManager + KernelClient
r10285 def cleanup_connection_file(self):
"""Cleanup connection file *if we wrote it*
Will not raise if the connection file was already removed somehow.
"""
if self._connection_file_written:
# cleanup connection files on full shutdown of kernel we started
self._connection_file_written = False
try:
os.remove(self.connection_file)
except (IOError, OSError, AttributeError):
pass
def cleanup_ipc_files(self):
"""Cleanup ipc files if we wrote them."""
if self.transport != 'ipc':
return
MinRK
add control channel...
r10296 for port in self.ports:
MinRK
split KernelManager into KernelManager + KernelClient
r10285 ipcfile = "%s-%i" % (self.ip, port)
try:
os.remove(ipcfile)
except (IOError, OSError):
pass
def write_connection_file(self):
"""Write connection info to JSON dict in self.connection_file."""
if self._connection_file_written:
return
MinRK
add control channel...
r10296
self.connection_file, cfg = write_connection_file(self.connection_file,
MinRK
split KernelManager into KernelManager + KernelClient
r10285 transport=self.transport, ip=self.ip, key=self.session.key,
stdin_port=self.stdin_port, iopub_port=self.iopub_port,
MinRK
add control channel...
r10296 shell_port=self.shell_port, hb_port=self.hb_port,
control_port=self.control_port,
MinRK
signature_scheme lives in Session...
r11816 signature_scheme=self.session.signature_scheme,
MinRK
add control channel...
r10296 )
MinRK
split KernelManager into KernelManager + KernelClient
r10285 # write_connection_file also sets default ports:
MinRK
add control channel...
r10296 for name in port_names:
setattr(self, name, cfg[name])
MinRK
split KernelManager into KernelManager + KernelClient
r10285
self._connection_file_written = True
def load_connection_file(self):
"""Load connection info from JSON dict in self.connection_file."""
with open(self.connection_file) as f:
cfg = json.loads(f.read())
self.transport = cfg.get('transport', 'tcp')
self.ip = cfg['ip']
MinRK
add control channel...
r10296 for name in port_names:
setattr(self, name, cfg[name])
MinRK
add signature_scheme to Session and connection files...
r11656 if 'key' in cfg:
self.session.key = str_to_bytes(cfg['key'])
if cfg.get('signature_scheme'):
self.session.signature_scheme = cfg['signature_scheme']
MinRK
split KernelManager into KernelManager + KernelClient
r10285
MinRK
move connect_[channel] to ConnectionFileMixin
r10324 #--------------------------------------------------------------------------
# Creating connected sockets
#--------------------------------------------------------------------------
def _make_url(self, channel):
"""Make a ZeroMQ URL for a given channel."""
transport = self.transport
ip = self.ip
port = getattr(self, '%s_port' % channel)
if transport == 'tcp':
return "tcp://%s:%i" % (ip, port)
else:
return "%s://%s-%s" % (transport, ip, port)
def _create_connected_socket(self, channel, identity=None):
"""Create a zmq Socket and connect it to the kernel."""
url = self._make_url(channel)
socket_type = channel_socket_types[channel]
MinRK
adjustments to notebook app logging...
r14645 self.log.debug("Connecting to: %s" % url)
MinRK
move connect_[channel] to ConnectionFileMixin
r10324 sock = self.context.socket(socket_type)
if identity:
sock.identity = identity
sock.connect(url)
return sock
def connect_iopub(self, identity=None):
"""return zmq Socket connected to the IOPub channel"""
sock = self._create_connected_socket('iopub', identity=identity)
sock.setsockopt(zmq.SUBSCRIBE, b'')
return sock
def connect_shell(self, identity=None):
"""return zmq Socket connected to the Shell channel"""
return self._create_connected_socket('shell', identity=identity)
def connect_stdin(self, identity=None):
"""return zmq Socket connected to the StdIn channel"""
return self._create_connected_socket('stdin', identity=identity)
def connect_hb(self, identity=None):
"""return zmq Socket connected to the Heartbeat channel"""
return self._create_connected_socket('hb', identity=identity)
def connect_control(self, identity=None):
"""return zmq Socket connected to the Heartbeat channel"""
return self._create_connected_socket('control', identity=identity)
MinRK
split KernelManager into KernelManager + KernelClient
r10285
MinRK
define and test IPython.kernel public API
r9376 __all__ = [
'write_connection_file',
'get_connection_file',
'find_connection_file',
'get_connection_info',
'connect_qtconsole',
'tunnel_to_kernel',
MinRK
split KernelManager into KernelManager + KernelClient
r10285 ]