Repository: ipython/ipython

Issue:
Error when use "ipython notebook" in win7 64 with python2.7.3 64.
when i use ipython notebook, it cannot run currently(ipython works well), and shows:
Traceback (most recent call last):
  File "c:\python27\Scripts\ipython-script.py", line 9, in <module>
    load_entry_point('ipython==1.1.0', 'console_scripts', 'ipython')()
  File "c:\python27\lib\site-packages\IPython__init__.py", line 118, in start_
python
    return launch_new_instance(argv=argv, *_kwargs)
  File "c:\python27\lib\site-packages\IPython\config\application.py", line 544,
in launch_instance
    app.initialize(argv)
  File "<string>", line 2, in initialize
  File "c:\python27\lib\site-packages\IPython\config\application.py", line 89,
n catch_config_error
    return method(app, *args, *_kwargs)
  File "c:\python27\lib\site-packages\IPython\terminal\ipapp.py", line 312, in
nitialize
    super(TerminalIPythonApp, self).initialize(argv)
  File "<string>", line 2, in initialize
  File "c:\python27\lib\site-packages\IPython\config\application.py", line 89,
n catch_config_error
    return method(app, _args, *_kwargs)
  File "c:\python27\lib\site-packages\IPython\core\application.py", line 362, i
 initialize
    self.parse_command_line(argv)
  File "c:\python27\lib\site-packages\IPython\terminal\ipapp.py", line 307, in
arse_command_line
    return super(TerminalIPythonApp, self).parse_command_line(argv)
  File "<string>", line 2, in parse_command_line
  File "c:\python27\lib\site-packages\IPython\config\application.py", line 89,
n catch_config_error
    return method(app, _args, *_kwargs)
  File "c:\python27\lib\site-packages\IPython\config\application.py", line 474,
in parse_command_line
    return self.initialize_subcommand(subc, subargv)
  File "<string>", line 2, in initialize_subcommand
  File "c:\python27\lib\site-packages\IPython\config\application.py", line 89,
n catch_config_error
    return method(app, _args, *_kwargs)
  File "c:\python27\lib\site-packages\IPython\config\application.py", line 412,
in initialize_subcommand
    self.subapp.initialize(argv)
  File "<string>", line 2, in initialize
  File "c:\python27\lib\site-packages\IPython\config\application.py", line 89,
n catch_config_error
    return method(app, _args, *_kwargs)
  File "c:\python27\lib\site-packages\IPython\html\notebookapp.py", line 665, i
 initialize
    self.init_webapp()
  File "c:\python27\lib\site-packages\IPython\html\notebookapp.py", line 546, i
 init_webapp
    self.http_server.listen(port, self.ip)
  File "c:\python27\lib\site-packages\tornado\tcpserver.py", line 117, in liste

```
sockets = bind_sockets(port, address=address)
```

  File "c:\python27\lib\site-packages\tornado\netutil.py", line 90, in bind_soc
ets
    sock.bind(sockaddr)
  File "c:\python27\lib\socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 10013]

If you suspect this is an IPython bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@scipy.org

I just install ipython with"pip install ipython".

Is it a bug or just I do not config it right?

Thank you .

---

Have solved it with --port parameter



Discussion:
Can we expand the solution a bit more, for other people who might encounter the same problem and read this?

From a quick search, it appears that this error can occur either when you try to bind to a port that you need admin permissions for (that's ports below 1024 on Linux - is that the same on Windows), or when another process has already bound to that port (which we should automatically detect and handle).


Resolving patch:
diff --git a/IPython/html/notebookapp.py b/IPython/html/notebookapp.py
--- a/IPython/html/notebookapp.py
+++ b/IPython/html/notebookapp.py
@@ -112,7 +112,7 @@ def random_ports(port, n):
     for i in range(min(5, n)):
         yield port + i
     for i in range(n-5):
-        yield port + random.randint(-2*n, 2*n)
+        yield max(1, port + random.randint(-2*n, 2*n))
 
 def load_handlers(name):
     """Load the (URL pattern, handler) tuples for each component."""
@@ -590,9 +590,14 @@ def init_webapp(self):
                         break
                     # restore the monekypatch
                     socket.AI_ADDRCONFIG = saved_AI_ADDRCONFIG
-                if e.errno != errno.EADDRINUSE:
+                if e.errno == errno.EADDRINUSE:
+                    self.log.info('The port %i is already in use, trying another random port.' % port)
+                    continue
+                elif e.errno in (errno.EACCES, getattr(errno, 'WSAEACCES', errno.EACCES)):
+                    self.log.warn("Permission to listen on port %i denied" % port)
+                    continue
+                else:
                     raise
-                self.log.info('The port %i is already in use, trying another random port.' % port)
             else:
                 self.port = port
                 success = True


Test patch: