Bug: 232460268

Clone this repo:
  1. ed8dd23 Remove Python enables identical to global defaults. am: 9c3d6d8b36 am: 8b984d9640 am: a8d445d366 am: 3a1c64eb78 am: acf6749321 by Krzysztof Kosiński · 1 year, 7 months ago android14-dev android14-mainline-healthfitness-release android14-qpr1-release android14-qpr1-s2-release android14-qpr2-release android14-qpr2-s1-release android14-qpr2-s2-release android14-qpr2-s3-release main master aml_cfg_341510000 aml_hef_341114030 aml_hef_341311010 aml_hef_341415040 aml_hef_341512030 aml_rkp_341012000 aml_rkp_341015010 aml_rkp_341114000 aml_rkp_341311000 aml_rkp_341510000 android-14.0.0_r16 android-14.0.0_r17 android-14.0.0_r18 android-14.0.0_r19 android-14.0.0_r20 android-14.0.0_r21 android-14.0.0_r22 android-14.0.0_r23 android-14.0.0_r24 android-14.0.0_r25 android-14.0.0_r26 android-14.0.0_r27 android-14.0.0_r29 android-14.0.0_r30 android-14.0.0_r31 android-14.0.0_r32 android-14.0.0_r33
  2. acf6749 Remove Python enables identical to global defaults. am: 9c3d6d8b36 am: 8b984d9640 am: a8d445d366 am: 3a1c64eb78 by Krzysztof Kosiński · 1 year, 7 months ago main-16k-with-phones android-u-beta-1-gpl
  3. 3a1c64e Remove Python enables identical to global defaults. am: 9c3d6d8b36 am: 8b984d9640 am: a8d445d366 by Krzysztof Kosiński · 1 year, 7 months ago
  4. a8d445d Remove Python enables identical to global defaults. am: 9c3d6d8b36 am: 8b984d9640 by Krzysztof Kosiński · 1 year, 7 months ago
  5. 8b984d9 Remove Python enables identical to global defaults. am: 9c3d6d8b36 by Krzysztof Kosiński · 1 year, 7 months ago

Python portpicker module

PyPI version GH Action Status Travis CI org Status

This module is useful for finding unused network ports on a host. If you need legacy Python 2 support, use the 1.3.x releases.

This module provides a pure Python pick_unused_port() function. It can also be called via the command line for use in shell scripts.

If your code can accept a bound TCP socket rather than a port number consider using socket.bind(('localhost', 0)) to bind atomically to an available port rather than using this library at all.

There is a race condition between picking a port and your application code binding to it. The use of a port server by all of your test code to avoid that problem is recommended on loaded test hosts running many tests at a time.

Unless you are using a port server, subsequent calls to pick_unused_port() to obtain an additional port are not guaranteed to return a unique port.

What is the optional port server?

A port server is intended to be run as a daemon, for use by all processes running on the host. It coordinates uses of network ports by anything using a portpicker library. If you are using hosts as part of a test automation cluster, each one should run a port server as a daemon. You should set the PORTSERVER_ADDRESS=@unittest-portserver environment variable on all of your test runners so that portpicker makes use of it.

A sample port server is included. This portserver implementation works but has not spent time in production. If you use it with good results please report back so that this statement can be updated to reflect that. :)

A port server listens on a unix socket, reads a pid from a new connection, tests the ports it is managing and replies with a port assignment port for that pid. A port is only reclaimed for potential reassignment to another process after the process it was originally assigned to has died. Processes that need multiple ports can simply issue multiple requests and are guaranteed they will each be unique.

Typical usage:

import portpicker
test_port = portpicker.pick_unused_port()

Or from the command line:

TEST_PORT=`/path/to/portpicker.py $$`

Or, if portpicker is installed as a library on the system Python interpreter:

TEST_PORT=`python3 -m portpicker $$`

DISCLAIMER

This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google.