diff --git a/IPython/frontend/qt/console/ipythonqt.py b/IPython/frontend/qt/console/ipythonqt.py index 89b94fd..8d15cee 100644 --- a/IPython/frontend/qt/console/ipythonqt.py +++ b/IPython/frontend/qt/console/ipythonqt.py @@ -16,10 +16,10 @@ from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget from IPython.frontend.qt.kernelmanager import QtKernelManager #----------------------------------------------------------------------------- -# Constants +# Network Constants #----------------------------------------------------------------------------- -LOCALHOST = '127.0.0.1' +from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS #----------------------------------------------------------------------------- # Classes @@ -45,7 +45,7 @@ class MainWindow(QtGui.QMainWindow): self._app = app self._frontend = frontend self._existing = existing - if not existing: + if existing: self._may_close = may_close else: self._may_close = True @@ -144,12 +144,16 @@ def main(): rep_address=(args.ip, args.rep), hb_address=(args.ip, args.hb)) if not args.existing: + # if not args.ip in LOCAL_IPS+ALL_ALIAS: + # raise ValueError("Must bind a local ip, such as: %s"%LOCAL_IPS) + + kwargs = dict(ip=args.ip) if args.pure: - kernel_manager.start_kernel(ipython=False) + kwargs['ipython']=False elif args.pylab: - kernel_manager.start_kernel(pylab=args.pylab) - else: - kernel_manager.start_kernel() + kwargs['pylab']=args.pylab + + kernel_manager.start_kernel(**kwargs) kernel_manager.start_channels() local_kernel = (not args.existing) or args.ip == LOCALHOST diff --git a/IPython/zmq/entry_point.py b/IPython/zmq/entry_point.py index a69d821..f2e206e 100644 --- a/IPython/zmq/entry_point.py +++ b/IPython/zmq/entry_point.py @@ -16,6 +16,7 @@ import zmq from IPython.core.ultratb import FormattedTB from IPython.external.argparse import ArgumentParser from IPython.utils import io +from IPython.utils.localinterfaces import LOCALHOST from displayhook import DisplayHook from heartbeat import Heartbeat from iostream import OutStream @@ -40,7 +41,7 @@ def make_argument_parser(): kernel entry points. """ parser = ArgumentParser() - parser.add_argument('--ip', type=str, default='127.0.0.1', + parser.add_argument('--ip', type=str, default=LOCALHOST, help='set the kernel\'s IP address [default: local]') parser.add_argument('--xrep', type=int, metavar='PORT', default=0, help='set the XREP channel port [default: random]') diff --git a/IPython/zmq/frontend.py b/IPython/zmq/frontend.py index 4dc73c9..90f3530 100755 --- a/IPython/zmq/frontend.py +++ b/IPython/zmq/frontend.py @@ -17,6 +17,7 @@ import uuid import zmq import session import completer +from IPython.utils.localinterfaces import LOCALHOST #----------------------------------------------------------------------------- # Classes and functions @@ -168,7 +169,7 @@ class InteractiveClient(object): def main(): # Defaults #ip = '192.168.2.109' - ip = '127.0.0.1' + ip = LOCALHOST #ip = '99.146.222.252' port_base = 5575 connection = ('tcp://%s' % ip) + ':%i' diff --git a/IPython/zmq/heartbeat.py b/IPython/zmq/heartbeat.py index 28c85d0..9ff2a4a 100644 --- a/IPython/zmq/heartbeat.py +++ b/IPython/zmq/heartbeat.py @@ -17,6 +17,8 @@ from threading import Thread import zmq +from IPython.utils.localinterfaces import LOCALHOST + #----------------------------------------------------------------------------- # Code #----------------------------------------------------------------------------- @@ -25,7 +27,7 @@ import zmq class Heartbeat(Thread): "A simple ping-pong style heartbeat that runs in a thread." - def __init__(self, context, addr=('127.0.0.1', 0)): + def __init__(self, context, addr=(LOCALHOST, 0)): Thread.__init__(self) self.context = context self.addr = addr diff --git a/IPython/zmq/ipkernel.py b/IPython/zmq/ipkernel.py index fe9030b..f271305 100755 --- a/IPython/zmq/ipkernel.py +++ b/IPython/zmq/ipkernel.py @@ -534,12 +534,15 @@ class GTKKernel(Kernel): # Kernel main and launch functions #----------------------------------------------------------------------------- -def launch_kernel(xrep_port=0, pub_port=0, req_port=0, hb_port=0, +def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0, independent=False, pylab=False): """Launches a localhost kernel, binding to the specified ports. Parameters ---------- + ip : str, optional + The ip address the kernel will bind to. + xrep_port : int, optional The port to use for XREP channel. @@ -574,6 +577,10 @@ def launch_kernel(xrep_port=0, pub_port=0, req_port=0, hb_port=0, extra_arguments.append('--pylab') if isinstance(pylab, basestring): extra_arguments.append(pylab) + if ip is not None: + extra_arguments.append('--ip') + if isinstance(ip, basestring): + extra_arguments.append(ip) return base_launch_kernel('from IPython.zmq.ipkernel import main; main()', xrep_port, pub_port, req_port, hb_port, independent, extra_arguments) diff --git a/IPython/zmq/kernelmanager.py b/IPython/zmq/kernelmanager.py index 46e70cd..089e519 100644 --- a/IPython/zmq/kernelmanager.py +++ b/IPython/zmq/kernelmanager.py @@ -31,6 +31,7 @@ from zmq.eventloop import ioloop # Local imports. from IPython.utils import io +from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS from IPython.utils.traitlets import HasTraits, Any, Instance, Type, TCPAddress from session import Session @@ -38,8 +39,6 @@ from session import Session # Constants and exceptions #----------------------------------------------------------------------------- -LOCALHOST = '127.0.0.1' - class InvalidPortNumber(Exception): pass @@ -724,24 +723,26 @@ class KernelManager(HasTraits): """ xreq, sub, rep, hb = self.xreq_address, self.sub_address, \ self.rep_address, self.hb_address - if xreq[0] != LOCALHOST or sub[0] != LOCALHOST or \ - rep[0] != LOCALHOST or hb[0] != LOCALHOST: - raise RuntimeError("Can only launch a kernel on localhost." + if xreq[0] not in LOCAL_IPS or sub[0] not in LOCAL_IPS or \ + rep[0] not in LOCAL_IPS or hb[0] not in LOCAL_IPS: + raise RuntimeError("Can only launch a kernel on a local interface. " "Make sure that the '*_address' attributes are " - "configured properly.") - + "configured properly. " + "Currently valid addresses are: %s"%LOCAL_IPS + ) + self._launch_args = kw.copy() if kw.pop('ipython', True): from ipkernel import launch_kernel else: from pykernel import launch_kernel - self.kernel, xrep, pub, req, hb = launch_kernel( + self.kernel, xrep, pub, req, _hb = launch_kernel( xrep_port=xreq[1], pub_port=sub[1], req_port=rep[1], hb_port=hb[1], **kw) - self.xreq_address = (LOCALHOST, xrep) - self.sub_address = (LOCALHOST, pub) - self.rep_address = (LOCALHOST, req) - self.hb_address = (LOCALHOST, hb) + self.xreq_address = (xreq[0], xrep) + self.sub_address = (sub[0], pub) + self.rep_address = (rep[0], req) + self.hb_address = (hb[0], _hb) def shutdown_kernel(self, restart=False): """ Attempts to the stop the kernel process cleanly. If the kernel diff --git a/IPython/zmq/pykernel.py b/IPython/zmq/pykernel.py index 4938a2a..8a8298c 100755 --- a/IPython/zmq/pykernel.py +++ b/IPython/zmq/pykernel.py @@ -256,12 +256,15 @@ class Kernel(HasTraits): # Kernel main and launch functions #----------------------------------------------------------------------------- -def launch_kernel(xrep_port=0, pub_port=0, req_port=0, hb_port=0, +def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0, independent=False): """ Launches a localhost kernel, binding to the specified ports. Parameters ---------- + ip : str, optional + The ip address the kernel will bind to. + xrep_port : int, optional The port to use for XREP channel. @@ -286,9 +289,15 @@ def launch_kernel(xrep_port=0, pub_port=0, req_port=0, hb_port=0, (kernel_process, xrep_port, pub_port, req_port) where kernel_process is a Popen object and the ports are integers. """ + extra_arguments = [] + if ip is not None: + extra_arguments.append('--ip') + if isinstance(ip, basestring): + extra_arguments.append(ip) + return base_launch_kernel('from IPython.zmq.pykernel import main; main()', xrep_port, pub_port, req_port, hb_port, - independent) + independent, extra_arguments=extra_arguments) main = make_default_main(Kernel)