From cb953bfcc76a03127c7145d190625888b8a45504 Mon Sep 17 00:00:00 2001 From: MinRK Date: Mon, 30 Sep 2013 11:14:34 -0700 Subject: [PATCH 1/2] limit random ports to positive values --- IPython/html/notebookapp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IPython/html/notebookapp.py b/IPython/html/notebookapp.py index e5d2298bf..7f8f390d5 100644 --- 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.""" From 4d48a84c8ba250670cc47be0effd2284937bcc7f Mon Sep 17 00:00:00 2001 From: MinRK Date: Mon, 30 Sep 2013 11:20:42 -0700 Subject: [PATCH 2/2] catch EACCES when binding notebook app can come up for low ports on *ix, or user access control restrictions on Windows. closes #4308 --- IPython/html/notebookapp.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/IPython/html/notebookapp.py b/IPython/html/notebookapp.py index 7f8f390d5..206473cfb 100644 --- a/IPython/html/notebookapp.py +++ b/IPython/html/notebookapp.py @@ -590,9 +590,14 @@ class NotebookApp(BaseIPythonApplication): 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