Skip to main content

Utility that helps with local TCP ports management. It can find an unused TCP localhost port and remember the association.

Project description

PyPI Version Code Coverage

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

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.


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

sudo pip install port-for


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

$ port-for foo

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

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 35456

Remove an association:

$ sudo port-for --unbind foo
$ port-for --list 35456

Library usage

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

>>> port_for.select_random()

>>> 80 in port_for.available_good_ports()

>>> port_for.get_port()

>>> port_for.get_port("1234")

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

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

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

Dig into source code for more.


Development happens at github:

Issue tracker:


Install pipenv and –dev dependencies first, Then run:

pipenv run tbump [NEW_VERSION]

Project details

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-0.7.2.tar.gz (24.9 kB view hashes)

Uploaded source

Built Distribution

port_for-0.7.2-py3-none-any.whl (21.3 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page