Skip to main content

Utility that helps with local TCP ports managment. 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
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

Dig into source code for more.

Contributing

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

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

In order to run tests make sure you have tox installed, then type

tox

from the source checkout.

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.5.0.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

port_for-0.5.0-py2.py3-none-any.whl (18.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file port-for-0.5.0.tar.gz.

File metadata

  • Download URL: port-for-0.5.0.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for port-for-0.5.0.tar.gz
Algorithm Hash digest
SHA256 a945d9b9e58c19ed57a9ada959829eb83c618ece27eb796440b53cd6596b8a3e
MD5 9ba9fca4b8f1ecd7f2bcace072af0ddc
BLAKE2b-256 eaf78ae91a323fff4b634e5168376980390fc18773facf8b4080130fdec4d4c5

See more details on using hashes here.

File details

Details for the file port_for-0.5.0-py2.py3-none-any.whl.

File metadata

  • Download URL: port_for-0.5.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for port_for-0.5.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2f069b088956ff4c4e74752a3076c0c84f489210c4eb0e7efb8b4f48c05255cc
MD5 6096f99892b5e820b1fbc2e5474d1c2f
BLAKE2b-256 b093e4fddf90fb12054ec9c1a493c0b43612393cb9017d6a3dea9f0e01159ab2

See more details on using hashes here.

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