Utility that helps with local TCP ports managment. It can find an unused TCP localhost port and remember the association.
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.
If you’re looking for a temporary port then socket.bind((host, 0)) is your best bet:
>>> import socket >>> s = socket.socket() >>> s.bind(("", 0)) >>> s.getsockname() ('0.0.0.0', 54485)
port-for is necessary when you need persistent free local port number.
port-for is the exact opposite of s.bind((host, 0)) in the sense that it shouldn’t return ports that s.bind((host, 0)) may return (because such ports are likely to be temporary used by OS).
There are several rules port-for is trying to follow to find and return a new unused port:
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
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
>>> 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.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|File Name & Checksum SHA256 Checksum Help||Version||File Type||Upload Date|
|port_for-0.4-py2.py3-none-any.whl (21.2 kB) Copy SHA256 Checksum SHA256||3.6||Wheel||Jul 19, 2017|
|port-for-0.4.tar.gz (19.4 kB) Copy SHA256 Checksum SHA256||–||Source||Jul 19, 2017|