Skip to content

Improve support for dynamic port allocations #10665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
twhittock-disguise opened this issue Apr 1, 2025 · 1 comment · May be fixed by #10697
Open
1 task done

Improve support for dynamic port allocations #10665

twhittock-disguise opened this issue Apr 1, 2025 · 1 comment · May be fixed by #10697
Labels
enhancement good first issue Good for newcomers Hacktoberfest We think it's good for https://hacktoberfest.digitalocean.com/

Comments

@twhittock-disguise
Copy link

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
@Dreamsorcerer
Copy link
Member

I think propagating the port back up in that case makes sense, and we could certainly make the port attribute public.

@Dreamsorcerer Dreamsorcerer added good first issue Good for newcomers Hacktoberfest We think it's good for https://hacktoberfest.digitalocean.com/ labels Apr 1, 2025
twhittock-disguise added a commit to twhittock-disguise/aiohttp that referenced this issue Apr 5, 2025
@twhittock-disguise twhittock-disguise linked a pull request Apr 5, 2025 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement good first issue Good for newcomers Hacktoberfest We think it's good for https://hacktoberfest.digitalocean.com/
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants