Permalink
Browse files

Possible fix for GH-169

  • Loading branch information...
1 parent 0641149 commit cd37162666ee713076f79c77c4e49e555fbacc6a @minrk committed Oct 13, 2010
View
18 IPython/frontend/qt/console/ipythonqt.py
@@ -16,10 +16,10 @@
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 @@ def __init__(self, app, frontend, existing=False, may_close=True):
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
View
3 IPython/zmq/entry_point.py
@@ -16,6 +16,7 @@
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]')
View
3 IPython/zmq/frontend.py
@@ -17,6 +17,7 @@
import zmq
import session
import completer
+from IPython.utils.localinterfaces import LOCALHOST
#-----------------------------------------------------------------------------
# Classes and functions
@@ -168,7 +169,7 @@ def interact(self):
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'
View
4 IPython/zmq/heartbeat.py
@@ -17,6 +17,8 @@
import zmq
+from IPython.utils.localinterfaces import LOCALHOST
+
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------
@@ -25,7 +27,7 @@
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
View
9 IPython/zmq/ipkernel.py
@@ -534,12 +534,15 @@ def start(self):
# 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)
View
25 IPython/zmq/kernelmanager.py
@@ -31,15 +31,14 @@
# 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
#-----------------------------------------------------------------------------
# Constants and exceptions
#-----------------------------------------------------------------------------
-LOCALHOST = '127.0.0.1'
-
class InvalidPortNumber(Exception):
pass
@@ -724,24 +723,26 @@ def start_kernel(self, **kw):
"""
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
View
13 IPython/zmq/pykernel.py
@@ -256,12 +256,15 @@ def _symbol_from_context(self, context):
# 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)

0 comments on commit cd37162

Please sign in to comment.