Repository: ipython/ipython

Issue:
notebook crashes on startup
I am using Python 2.7.6 and Ipython 2.2.0
on another computer, I am using IPython 2.1.0 and notebooks work fine

when starting up a notebook I get 

```
chianti/test> npython
2014-08-25 15:31:43.321 [NotebookApp] Using existing profile dir: u'/home/ken/.ipython/profile_notebook'
2014-08-25 15:31:43.331 [NotebookApp] Using MathJax from CDN: https://cdn.mathjax.org/mathjax/latest/MathJax.js
Traceback (most recent call last):
  File "/usr/bin/ipython", line 5, in <module>
    start_ipython()
  File "/usr/lib/python2.7/site-packages/IPython/__init__.py", line 120, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/usr/lib/python2.7/site-packages/IPython/config/application.py", line 563, in launch_instance
    app.initialize(argv)
  File "<string>", line 2, in initialize
  File "/usr/lib/python2.7/site-packages/IPython/config/application.py", line 92, in catch_config_error
    return method(app, *args, **kwargs)
  File "/usr/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 320, in initialize
    super(TerminalIPythonApp, self).initialize(argv)
  File "<string>", line 2, in initialize
  File "/usr/lib/python2.7/site-packages/IPython/config/application.py", line 92, in catch_config_error
    return method(app, *args, **kwargs)
  File "/usr/lib/python2.7/site-packages/IPython/core/application.py", line 381, in initialize
    self.parse_command_line(argv)
  File "/usr/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 315, in parse_command_line
    return super(TerminalIPythonApp, self).parse_command_line(argv)
  File "<string>", line 2, in parse_command_line
  File "/usr/lib/python2.7/site-packages/IPython/config/application.py", line 92, in catch_config_error
    return method(app, *args, **kwargs)
  File "/usr/lib/python2.7/site-packages/IPython/config/application.py", line 475, in parse_command_line
    return self.initialize_subcommand(subc, subargv)
  File "<string>", line 2, in initialize_subcommand
  File "/usr/lib/python2.7/site-packages/IPython/config/application.py", line 92, in catch_config_error
    return method(app, *args, **kwargs)
  File "/usr/lib/python2.7/site-packages/IPython/config/application.py", line 413, in initialize_subcommand
    self.subapp.initialize(argv)
  File "<string>", line 2, in initialize
  File "/usr/lib/python2.7/site-packages/IPython/config/application.py", line 92, in catch_config_error
    return method(app, *args, **kwargs)
  File "/usr/lib/python2.7/site-packages/IPython/html/notebookapp.py", line 771, in initialize
    self.init_webapp()
  File "/usr/lib/python2.7/site-packages/IPython/html/notebookapp.py", line 658, in init_webapp
    self.http_server.listen(port, self.ip)
  File "/usr/lib/python2.7/site-packages/tornado/tcpserver.py", line 116, in listen
    sockets = bind_sockets(port, address=address)
  File "/usr/lib/python2.7/site-packages/tornado/netutil.py", line 89, in bind_sockets
    sock.bind(sockaddr)
  File "/usr/lib64/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 99] Cannot assign requested address
```



Discussion:
Does it work if you do `ipython notebook --ip=127.0.0.1`?

this is my email reply to minrk yesterday

Yes, it does.  Should I always include --ip=127.0.0.1?  Did not have to in the past

thanks,

Le 27 août 2014 à 15:30, kdere notifications@github.com a écrit :

> this is my email reply to minrk yesterday
> 
> Yes, it does. Should I always include --ip=127.0.0.1? Did not have to in the past
> 
> thanks,

You can set `c.NotebookApp.ip` in your profile. 

It might be due to change in logic that allow notebook to work on windows w/ sophos antivirus. 

The logic to have work « out of the box » is complex, and there a re always incompatible scenario. 
I guess you fall in one of them where you have to be explicit. 

Do you have any firewall/antvirus software/proxy ? 

> —
> Reply to this email directly or view it on GitHub.

On Wednesday, August 27, 2014 07:39:35 AM Matthias Bussonnier wrote:

> Le 27 août 2014 à 15:30, kdere notifications@github.com a écrit :
> 
> > this is my email reply to minrk yesterday
> > 
> > Yes, it does. Should I always include --ip=127.0.0.1? Did not have to in
> > the past
> > 
> > thanks,
> 
> You can set `c.NotebookApp.ip` in your profile.
> 
> It might be due to change in logic that allow notebook to work on windows w/
> sophos antivirus.
> 
> The logic to have work « out of the box » is complex, and there a re always
> incompatible scenario. I guess you fall in one of them where you have to be
> explicit.
> 
> Do you have any firewall/antvirus software/proxy ?
> 
> > —
> > Reply to this email directly or view it on GitHub.
> 
> ---
> 
> Reply to this email directly or view it on GitHub:
> https://github.com/ipython/ipython/issues/6355#issuecomment-53581936

My firewall is on my router.

IPython 2.1.0 notebooks use:
 "The IPython Notebook is running at: http://localhost:8888/"

Ken

@kdere did you get this figured out?

with IPython 2.1 I don't have to specify an ip address
with IPython 2.2 and 2.3 I need to specify --ip=127.0.0.1

@kdere what do you get if you try:

``` python
import socket
s = socket.socket()
s.bind(('localhost', 0))
```

running IPython 2.3.0
if I have already set --ip=127.0.0.0 those commands work - well, they don't throw off error messages
if not, I get the message that
port 8888 is already in use, try another random port


Resolving patch:
diff --git a/IPython/html/notebookapp.py b/IPython/html/notebookapp.py
--- a/IPython/html/notebookapp.py
+++ b/IPython/html/notebookapp.py
@@ -410,6 +410,20 @@ def _log_format_default(self):
     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 as e:
+            self.log.warn("Cannot bind to localhost, using 127.0.0.1 as default ip\n%s", e)
+            return '127.0.0.1'
+        else:
+            s.close()
+            return 'localhost'
 
     def _ip_changed(self, name, old, new):
         if new == u'*': self.ip = u''


Test patch: