-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add dynamic port binding #10697
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
base: master
Are you sure you want to change the base?
Add dynamic port binding #10697
Changes from all commits
e610227
634c3e7
8eb2d47
02ac026
dd911b7
c042770
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added :py:attr:`~aiohttp.web.TCPSite.port` accessor for dynamic port allocations in :class:`~aiohttp.web.TCPSite` -- by :user:`twhittock-disguise`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,6 +113,11 @@ def name(self) -> str: | |
host = "0.0.0.0" if not self._host else self._host | ||
return str(URL.build(scheme=scheme, host=host, port=self._port)) | ||
|
||
@property | ||
def port(self) -> int: | ||
"""Return the port number the server is bound to, useful for the dynamically allocated port (0).""" | ||
return self._port | ||
|
||
async def start(self) -> None: | ||
await super().start() | ||
loop = asyncio.get_event_loop() | ||
|
@@ -127,6 +132,10 @@ async def start(self) -> None: | |
reuse_address=self._reuse_address, | ||
reuse_port=self._reuse_port, | ||
) | ||
if self._port == 0: | ||
# Port 0 means bind to any port, so we need to set the attribute | ||
# to the port the server was actually bound to. | ||
self._port = self._server.sockets[0].getsockname()[1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't reassign this but track the bound port separately. This is so starting and stopping the TCP site multiple times would be able to get a new port allocated each time. |
||
|
||
|
||
class UnixSite(BaseSite): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,9 +256,21 @@ async def test_tcpsite_empty_str_host(make_runner: _RunnerMaker) -> None: | |
runner = make_runner() | ||
await runner.setup() | ||
site = web.TCPSite(runner, host="") | ||
assert site.port == 8080 | ||
assert site.name == "http://0.0.0.0:8080" | ||
|
||
|
||
async def test_tcpsite_ephemeral_port(make_runner: _RunnerMaker) -> None: | ||
runner = make_runner() | ||
await runner.setup() | ||
site = web.TCPSite(runner, port=0) | ||
|
||
await site.start() | ||
assert site.port != 0 | ||
assert site.name.startswith("http://0.0.0.0:") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we know the port, this could just compare the entire string.. |
||
await site.stop() | ||
|
||
|
||
def test_run_after_asyncio_run() -> None: | ||
called = False | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, avoid having the docstring title line exceed 72 chars. You can have a longer paragraph below.
Also, referencing a magic number is not useful to readers unfamiliar with it. Use the ephemeral terminology so people could at least google it. Although, I would probably not even mention it since the underlying implementation details aren't really important to the end-users who'd just use this unconditionally.