diff options
| -rw-r--r-- | CHANGES.rst | 5 | ||||
| -rw-r--r-- | cherrypy/process/servers.py | 43 | ||||
| -rw-r--r-- | cherrypy/test/test_core.py | 19 | ||||
| -rw-r--r-- | cherrypy/wsgiserver/__init__.py | 4 |
4 files changed, 57 insertions, 14 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 63aeb78c..4c9b1d8e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,8 @@ +v8.7.0 +----- + +* #645: Setting a bind port of 0 will bind to an ephemeral port. + v8.6.0 ----- diff --git a/cherrypy/process/servers.py b/cherrypy/process/servers.py index bde39cd4..919d670b 100644 --- a/cherrypy/process/servers.py +++ b/cherrypy/process/servers.py @@ -155,15 +155,8 @@ class ServerAdapter(object): def start(self): """Start the HTTP server.""" - if self.bind_addr is None: - on_what = 'unknown interface (dynamic?)' - elif isinstance(self.bind_addr, tuple): - on_what = self._get_base() - else: - on_what = 'socket file: %s' % self.bind_addr - if self.running: - self.bus.log('Already serving on %s' % on_what) + self.bus.log('Already serving on %s' % self.description) return self.interrupt = None @@ -182,13 +175,26 @@ class ServerAdapter(object): self.wait() self.running = True - self.bus.log('Serving on %s' % on_what) + self.bus.log('Serving on %s' % self.description) start.priority = 75 + @property + def description(self): + """ + A description about where this server is bound. + """ + if self.bind_addr is None: + on_what = 'unknown interface (dynamic?)' + elif isinstance(self.bind_addr, tuple): + on_what = self._get_base() + else: + on_what = 'socket file: %s' % self.bind_addr + return on_what + def _get_base(self): if not self.httpserver: return '' - host, port = self.bind_addr + host, port = self.bound_addr if getattr(self.httpserver, 'ssl_adapter', None): scheme = 'https' if port != 443: @@ -237,8 +243,19 @@ class ServerAdapter(object): # Wait for port to be occupied if not running via socket-activation # (for socket-activation the port will be managed by systemd ) if isinstance(self.bind_addr, tuple): - host, port = self.bind_addr - wait_for_occupied_port(host, port) + wait_for_occupied_port(*self.bound_addr) + + @property + def bound_addr(self): + """ + The bind address, or if it's an ephemeral port and the + socket has been bound, return the actual port bound. + """ + host, port = self.bind_addr + if port == 0 and self.httpserver.socket: + # Bound to ephemeral port. Get the actual port allocated. + port = self.httpserver.socket.getsockname()[1] + return host, port def stop(self): """Stop the HTTP server.""" @@ -247,7 +264,7 @@ class ServerAdapter(object): self.httpserver.stop() # Wait for the socket to be truly freed. if isinstance(self.bind_addr, tuple): - wait_for_free_port(*self.bind_addr) + wait_for_free_port(*self.bound_addr) self.running = False self.bus.log('HTTP Server %s shut down' % self.httpserver) else: diff --git a/cherrypy/test/test_core.py b/cherrypy/test/test_core.py index 0140bef4..a104216f 100644 --- a/cherrypy/test/test_core.py +++ b/cherrypy/test/test_core.py @@ -757,3 +757,22 @@ class ErrorTests(helper.CPWebCase): self.getPage('/stat/missing') self.assertStatus(404) self.assertInBody('No such file or directory') + + +class TestBinding: + def test_bind_ephemeral_port(self): + """ + A server configured to bind to port 0 will bind to an ephemeral + port and indicate that port number on startup. + """ + cherrypy.config.reset() + bind_ephemeral_conf = { + 'server.socket_port': 0, + } + cherrypy.config.update(bind_ephemeral_conf) + cherrypy.engine.start() + assert cherrypy.server.bound_addr != cherrypy.server.bind_addr + _host, port = cherrypy.server.bound_addr + assert port > 0 + cherrypy.engine.stop() + assert cherrypy.server.bind_addr == cherrypy.server.bound_addr diff --git a/cherrypy/wsgiserver/__init__.py b/cherrypy/wsgiserver/__init__.py index 0d46cb22..7cdf23f7 100644 --- a/cherrypy/wsgiserver/__init__.py +++ b/cherrypy/wsgiserver/__init__.py @@ -2052,11 +2052,13 @@ class HTTPServer(object): if self.ssl_adapter is not None: self.socket = self.ssl_adapter.bind(self.socket) + host, port = self.bind_addr[:2] + # If listening on the IPV6 any address ('::' = IN6ADDR_ANY), # activate dual-stack. See # https://github.com/cherrypy/cherrypy/issues/871. if (hasattr(socket, 'AF_INET6') and family == socket.AF_INET6 - and self.bind_addr[0] in ('::', '::0', '::0.0.0.0')): + and host in ('::', '::0', '::0.0.0.0')): try: self.socket.setsockopt( socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) |
