Dynamic selection for network service ports?

Peter J. Holzer hjp-python at hjp.at
Tue Apr 30 16:54:07 EDT 2019

On 2019-04-30 20:37:58 +0200, Markus Elfring wrote:
> > In Python, there's a certain amount of support. You can attempt to
> > bind to a port, and if you fail, try the next one in a sequence.
> * The zero seems to be also an usable parameter here.

Yes. This gives you a random port (just like a listen without a previous

But as Chris already wrote: The challenge is to communicate this port to
the client. If your server listens on a random port how does the client
know which port to connect to?

I know of two general purpose mechanisms: 

* Portmap: This is a Unix server where you can register your services
  and the client can then ask for the port number of a given service. 
  This was popular for Sun-RPC based services (e.g. NFS) in the late
  1980s/early 1990s, but I think it is mostly obsolete, now. Plus, if
  you run it on the open internet, your ISP might notify you that this
  is Not A Good Idea(TM). (Windows has a similar mechanism, but I've
  never used it)
* DNS: DNS has SRV (service) records, which include the host, port
  number and priority. So a client would ask "where do I find TCP service
  foo  for domain example.com?" and the DNS server might reply with:
  (_foo._tcp.example.com SRV 10 0 12345 server1.example,com,
   _foo._tcp.example.com SRV 20 0 54321 server2.example,net)
  The client would then first try server1.example,com:12345 and then
  To use this you need to set up a DNS server which allows updates (if
  you use AD, you already have one, if you use bind, it's not too hard).

Container orchestration systems like Kubernetes also include such
registration services.


   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | hjp at hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20190430/d0aa3688/attachment.sig>

More information about the Python-list mailing list