diff options
| author | Jason R. Coombs <jaraco@jaraco.com> | 2016-12-31 15:03:34 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-12-31 15:03:34 -0500 |
| commit | e0128437a00ee58b574b71d5b918f44de9ccfdc3 (patch) | |
| tree | 4f737d3f5b5f0bb32d18e235c3baa52574eab7d5 /cherrypy/process | |
| parent | f005c0aa681b2550386d12d8a6ef4cd2da1e05fd (diff) | |
| parent | c5cc9cf6648fb91dd1a07b9f8bc272d8faad78fe (diff) | |
| download | cherrypy-git-8.7.0.tar.gz | |
Merge pull request #1542 from cherrypy/feature/bind-ephemeralv8.7.0
Allow binding to an ephemeral port. Fixes #645.
Diffstat (limited to 'cherrypy/process')
| -rw-r--r-- | cherrypy/process/servers.py | 43 |
1 files changed, 30 insertions, 13 deletions
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: |
