Skip to main content
Latest PyPI version Wheel Status Supported Python Versions License

port-for is a command-line utility and a python library that helps with local TCP ports management.

It can find an unused TCP localhost port and remember the association:

$ sudo port-for foo
37987

This can be useful when you are installing a stack of software with multiple parts needing port numbers.

There are several rules port-for is trying to follow to find and return a new unused port:

  1. Port must be unused: port-for checks this by trying to connect to the port and to bind to it.

  2. Port must be IANA unassigned and otherwise not well-known: this is acheived by maintaining unassigned ports list (parsed from IANA and Wikipedia).

  3. Port shouldn’t be inside ephemeral port range. This is important because ports from ephemeral port range can be assigned temporary by OS (e.g. by machine’s IP stack) and this may prevent service restart in some circumstances. port-for doesn’t return ports from ephemeral port ranges configured at the current machine.

  4. Other heuristics are also applied: port-for tries to return a port from larger port ranges; it also doesn’t return ports that are too close to well-known ports.

Installation

System-wide using easy_install (something like python-setuptools should be installed):

sudo pip install port-for

or:

sudo easy_install port-for

or inside a virtualenv:

pip install port-for

Script usage

port-for <foo> script finds an unused port and associates it with <foo>. Subsequent calls return the same port number.

This utility doesn’t actually bind the port or otherwise prevents the port from being taken by another software. It tries to select a port that is less likely to be used by another software (and that is unused at the time of calling of course). Utility also makes sure that port-for bar won’t return the same port as port-for foo on the same machine.

$ sudo port-for foo
37987

$ port-for foo
37987

You may want to develop some naming conventions (e.g. prefix your app names) in order to enable multiple sites on the same server:

$ sudo port-for example.com/apache
35456

Please note that port-for script requires read and write access to /etc/port-for.conf. This usually means regular users can read port values but sudo is required to associate a new port.

List all associated ports:

$ port-for --list
foo: 37987
example.com/apache: 35456

Remove an association:

$ sudo port-for --unbind foo
$ port-for --list
example.com/apache: 35456

Library usage

>>> import port_for
>>> port_for.select_random()
37774

>>> port_for.select_random()
48324

>>> 80 in port_for.available_good_ports()
False

>>> port_for.get_port()
34455

>>> port_for.get_port("1234")
1234

>>> port_for.get_port((2000, 3000))
2345

>>> port_for.get_port({4001, 4003, 4005})
4005

>>> port_for.get_port([{4000, 4001}, (4100, 4200)])
4111

Dig into source code for more.

Contributing

Development happens at github: https://github.com/fizyk/port-for/

Issue tracker: https://github.com/fizyk/port-for/issues/new

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

port_for-1.1.0.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

port_for-1.1.0-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file port_for-1.1.0.tar.gz.

File metadata

  • Download URL: port_for-1.1.0.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for port_for-1.1.0.tar.gz
Algorithm Hash digest
SHA256 b9e949f0ec7101094c4b0f096fad80d0aa90818854fa69a9d750fa4fd1e7b4ba
MD5 7ccf441479f072fe395a99a890a21e1c
BLAKE2b-256 85dbcb50b1dd5ce6d1ffdfb93ff358c714c57367cc59beafe14209c21c0b3408

See more details on using hashes here.

Provenance

The following attestation bundles were made for port_for-1.1.0.tar.gz:

Publisher: pypi.yml on fizyk/port-for

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file port_for-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: port_for-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for port_for-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 99c543583dc33942b64ae5e3b80d71c536bd2eb1c568730d533776e0d0ff9264
MD5 9d224734c0ab4f7e49b595d43faa474b
BLAKE2b-256 5c8b823aec43117b17fcecd241b3c893c4e6f9156a878b139b2d684d71fe25f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for port_for-1.1.0-py3-none-any.whl:

Publisher: pypi.yml on fizyk/port-for

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 files

1.0.0

2 files

0.7.4

2 files

0.7.3

2 files

0.7.2

2 files

0.7.1

2 files

0.7.0

2 files

0.6.3

2 files

0.6.2

2 files

0.6.1

2 files

0.6.0

2 files

0.5.0

2 files

0.4

2 files

0.3.1

1 file

0.3

1 file

0.2

1 file

0.1

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page