Open
Description
Is your feature request related to a problem?
When working with TCPSite and DNS-SD (aka mDNS, Zeroconf, Bonjour) services, it is useful to allocate a dynamic port number (as port discovery is handled by the DNS-SD system)
In order to do that today, one must write code like the following:
site = web.TCPSite(runner, '0.0.0.0', 0) # Dynamic free port chosen by the kernel
await site.start() # connection is opened here
port = site._server.sockets[0].getsockname()[1] # Get the port number
print(f"Serving on http://0.0.0.0:{port}") # e.g. "http://0.0.0.0:51234"
This is problematic because it encourages access to the internal _server
field on the site object.
Describe the solution you'd like
In TCPSite.start, the server socket could be queried like the above and stored on a public property.
Alternatively, a public port
property on the TCPSite which does the above would be good for me.
I would expect it to be an error to access the port property before the socket is bound.
Describe alternatives you've considered
My workaround descirbed in the first section works, but is not discoverable and fragile.
Related component
Server
Additional context
No response
Code of Conduct
- I agree to follow the aio-libs Code of Conduct