From c605ea44cc86a12874b3c6bc90b50fcd8b3f4baf Mon Sep 17 00:00:00 2001 From: Min RK Date: Fri, 13 Feb 2015 10:32:45 -0800 Subject: [PATCH] fallback on 127.0.0.1 if localhost cannot be bound Some (broken) systems cannot bind to localhost. This handles that failure and falls back to 127 as the default --- IPython/html/notebookapp.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/IPython/html/notebookapp.py b/IPython/html/notebookapp.py index d1ed2d7b7..e53c52238 100644 --- a/IPython/html/notebookapp.py +++ b/IPython/html/notebookapp.py @@ -410,6 +410,19 @@ class NotebookApp(BaseIPythonApplication): ip = Unicode('localhost', config=True, help="The IP address the notebook server will listen on." ) + def _ip_default(self): + """Return localhost if available, 127.0.0.1 otherwise. + + On some (horribly broken) systems, localhost cannot be bound. + """ + s = socket.socket() + try: + s.bind(('localhost', 0)) + except socket.error: + return '127.0.0.1' + else: + s.close() + return 'localhost' def _ip_changed(self, name, old, new): if new == u'*': self.ip = u''