Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The basic idea is:</p> <ul> <li>Bind to port 0.</li> <li>Ask the socket which port you ended up with.</li> <li>Report that port somehow.</li> </ul> <p>The first part depends radically on how you're building, starting, and configuring your servers. CherryPy is hugely flexible, and the details will be different depending on how you're using it.</p> <p>The second part requires that you be using some mechanism where you're writing the top-level code that starts the servers, and <em>not</em> one of the mechanisms that starts them automatically without any of your code. That's because, before starting the server, you will need to manually <a href="http://docs.cherrypy.org/dev/refman/wsgiserver/init.html#cherrypy.wsgiserver.HTTPServer.bind" rel="nofollow"><code>bind</code></a> the server to get its socket. Once you have a created and bound socket, <a href="http://docs.python.org/3/library/socket.html#socket.socket.getsockname" rel="nofollow"><code>getsockname</code></a> will give you the address and port it ended up bound to.</p> <p>The third part can obviously be as easy or as hard as you want.</p> <hr> <p>Here's a trivial example, which you can even do from the interactive interpreter:</p> <pre><code>def app(env, start): start('200 OK', [('Content-type', 'text/plain')]) return ['Hello, world!'] server = cherrypy.wsgiserver.CherryPyWSGIServer(('0.0.0.0', 0), app, server_name='localhost') server.bind(socket.AF_INET, socket.SOCK_STREAM) addr, port = server.socket.getsockname() print(port) server.start() </code></pre> <hr> <p>You may also want to take a look at <a href="https://cherrypy.readthedocs.org/en/latest/pkg/cherrypy.process.html?highlight=serveradapter#cherrypy.process.servers.ServerAdapter" rel="nofollow"><code>cherrypy.process.servers</code></a> and its <code>ServerAdapter</code>, which lets you manage multiple servers from a single place, instead of having a bunch of independent servers that you have to manually coordinate.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload