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
This commit is contained in:
Min RK 2015-02-13 10:32:45 -08:00
parent ca95e914cb
commit c605ea44cc

View File

@ -410,6 +410,19 @@ class NotebookApp(BaseIPythonApplication):
ip = Unicode('localhost', config=True, ip = Unicode('localhost', config=True,
help="The IP address the notebook server will listen on." 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): def _ip_changed(self, name, old, new):
if new == u'*': self.ip = u'' if new == u'*': self.ip = u''