Repository: ipython/ipython

Issue:
[Notebook] Ipython Kernel does not start if disconnected from internet(/network?)
Hi,

If I am not connected to the internet, I cannot execute "ipython notebook". sucessfully.

I get the following error.
gaierror: [Errno -9] Address family for hostname not supported

(full traceback here: http://pastebin.com/sfEgXay8)

If I am connected to the internet, everything works as expected. I do not have a network available that is not connected to the internet, to test if a network connection or internet is required. (I suspect its just a network connection that is required)



Discussion:
Do you have any IPython config?  Try starting the notebook with `--debug`.

$ ipython notebook --debug
[NotebookApp] Config changed:
[NotebookApp] {'Application': {'log_level': 10}}
[NotebookApp] Using existing profile dir: u'/home/peter/.config/ipython/profile_default'
[NotebookApp] Searching path [u'/home/peter/code/pyTools/notebooks', u'/home/peter/.config/ipython/profile_default'] for config files
[NotebookApp] Attempting to load config file: ipython_config.py
[NotebookApp] Loaded config file: /home/peter/.config/ipython/profile_default/ipython_config.py
[NotebookApp] Attempting to load config file: ipython_notebook_config.py
[NotebookApp] Loaded config file: /home/peter/.config/ipython/profile_default/ipython_notebook_config.py
[NotebookApp] Serving notebooks from /home/peter/code/pyTools/notebooks
[NotebookApp] Adding cluster profile 'default'
... now traceback as before

I made one default profile (resides in ~/.config/ipython/profile_default/). I do not remember changing anything in this profile. But I am happy to provide any files you need. I had a script in startup (thats why I made the profile) but if I remove it from the startup directory, I still get the same error.

try:

``` python
from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS, PUBLIC_IPS
print LOCALHOST, LOCAL_IPS, PUBLIC_IPS
```

this is due to the use of AI_ADDRCONFIG in tornado, see https://github.com/facebook/tornado/pull/593


Resolving patch:
diff --git a/IPython/frontend/html/notebook/notebookapp.py b/IPython/frontend/html/notebook/notebookapp.py
--- a/IPython/frontend/html/notebook/notebookapp.py
+++ b/IPython/frontend/html/notebook/notebookapp.py
@@ -40,6 +40,7 @@
 from zmq.eventloop import ioloop
 ioloop.install()
 
+import tornado
 from tornado import httpserver
 from tornado import web
 
@@ -522,6 +523,30 @@ def init_webapp(self):
             try:
                 self.http_server.listen(port, self.ip)
             except socket.error as e:
+                # XXX: remove the e.errno == -9 block when we require
+                # tornado >= 3.0
+                if e.errno == -9 and tornado.version_info[0] < 3:
+                    # The flags passed to socket.getaddrinfo from
+                    # tornado.netutils.bind_sockets can cause "gaierror:
+                    # [Errno -9] Address family for hostname not supported"
+                    # when the interface is not associated, for example.
+                    # Changing the flags to exclude socket.AI_ADDRCONFIG does
+                    # not cause this error, but the only way to do this is to
+                    # monkeypatch socket to remove the AI_ADDRCONFIG attribute
+                    saved_AI_ADDRCONFIG = socket.AI_ADDRCONFIG
+                    self.log.warn('Monkeypatching socket to fix tornado bug')
+                    del(socket.AI_ADDRCONFIG)
+                    try:
+                        # retry the tornado call without AI_ADDRCONFIG flags
+                        self.http_server.listen(port, self.ip)
+                    except socket.error as e2:
+                        e = e2
+                    else:
+                        self.port = port
+                        success = True
+                        break
+                    # restore the monekypatch
+                    socket.AI_ADDRCONFIG = saved_AI_ADDRCONFIG
                 if e.errno != errno.EADDRINUSE:
                     raise
                 self.log.info('The port %i is already in use, trying another random port.' % port)


Test patch: