port-for


Nameport-for JSON
Version 0.7.4 PyPI version JSON
download
home_pageNone
SummaryUtility that helps with local TCP ports management. It can find an unused TCP localhost port and remember the association.
upload_time2024-10-09 12:28:38
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseCopyright (c) 2012 Mikhail Korobov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords port posix
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            ========
port-for
========

.. image:: https://img.shields.io/pypi/v/port-for.svg
    :target: https://pypi.python.org/pypi/port-for/
    :alt: Latest PyPI version

.. image:: https://img.shields.io/pypi/wheel/port-for.svg
    :target: https://pypi.python.org/pypi/port-for/
    :alt: Wheel Status

.. image:: https://img.shields.io/pypi/pyversions/port-for.svg
    :target: https://pypi.python.org/pypi/port-for/
    :alt: Supported Python Versions

.. image:: https://img.shields.io/pypi/l/port-for.svg
    :target: https://pypi.python.org/pypi/port-for/
    :alt: 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.

.. note::

    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:

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/kmike/port-for/

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

Release
=======

Install pipenv and --dev dependencies first, Then run:

.. code-block::

    pipenv run tbump [NEW_VERSION]

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "port-for",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "Grzegorz \u015aliwi\u0144ski <fizyk+pypi@fizyk.dev>",
    "keywords": "port, posix",
    "author": null,
    "author_email": "Mikhail Korobov <kmike84@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/f6/84/ad5114c85217426d7a5170a74a6f9d6b724df117c2f3b75e41fc9d6c6811/port_for-0.7.4.tar.gz",
    "platform": null,
    "description": "========\nport-for\n========\n\n.. image:: https://img.shields.io/pypi/v/port-for.svg\n    :target: https://pypi.python.org/pypi/port-for/\n    :alt: Latest PyPI version\n\n.. image:: https://img.shields.io/pypi/wheel/port-for.svg\n    :target: https://pypi.python.org/pypi/port-for/\n    :alt: Wheel Status\n\n.. image:: https://img.shields.io/pypi/pyversions/port-for.svg\n    :target: https://pypi.python.org/pypi/port-for/\n    :alt: Supported Python Versions\n\n.. image:: https://img.shields.io/pypi/l/port-for.svg\n    :target: https://pypi.python.org/pypi/port-for/\n    :alt: License\n\n\n\n``port-for`` is a command-line utility and a python library that\nhelps with local TCP ports management.\n\nIt can find an unused TCP localhost port and remember the association::\n\n    $ sudo port-for foo\n    37987\n\nThis can be useful when you are installing a stack of software\nwith multiple parts needing port numbers.\n\n.. note::\n\n    If you're looking for a temporary port then ``socket.bind((host, 0))``\n    is your best bet::\n\n        >>> import socket\n        >>> s = socket.socket()\n        >>> s.bind((\"\", 0))\n        >>> s.getsockname()\n        ('0.0.0.0', 54485)\n\n    ``port-for`` is necessary when you need *persistent* free local port number.\n\n    ``port-for`` is the exact opposite of ``s.bind((host, 0))``\n    in the sense that it shouldn't return ports that ``s.bind((host, 0))``\n    may return (because such ports are likely to be temporary used by OS).\n\n\nThere are several rules ``port-for`` is trying to follow to find and\nreturn a new unused port:\n\n1) Port must be unused: ``port-for`` checks this by trying to connect\n   to the port and to bind to it.\n\n2) Port must be IANA unassigned and otherwise not well-known:\n   this is acheived by maintaining unassigned ports list\n   (parsed from IANA and Wikipedia).\n\n3) Port shouldn't be inside ephemeral port range.\n   This is important because ports from ephemeral port range can\n   be assigned temporary by OS (e.g. by machine's IP stack) and\n   this may prevent service restart in some circumstances.\n   ``port-for`` doesn't return ports from ephemeral port ranges\n   configured at the current machine.\n\n4) Other heuristics are also applied: ``port-for`` tries to return\n   a port from larger port ranges; it also doesn't return ports that are\n   too close to well-known ports.\n\nInstallation\n============\n\nSystem-wide using easy_install (something like ``python-setuptools``\nshould be installed)::\n\n    sudo pip install port-for\n\nor::\n\n    sudo easy_install port-for\n\nor inside a virtualenv::\n\n    pip install port-for\n\nScript usage\n============\n\n``port-for <foo>`` script finds an unused port and associates\nit with ``<foo>``. Subsequent calls return the same port number.\n\nThis utility doesn't actually bind the port or otherwise prevents the\nport from being taken by another software. It tries to select\na port that is less likely to be used by another software\n(and that is unused at the time of calling of course). Utility also makes\nsure that ``port-for bar`` won't return the same port as ``port-for foo``\non the same machine.\n\n::\n\n    $ sudo port-for foo\n    37987\n\n    $ port-for foo\n    37987\n\nYou may want to develop some naming conventions (e.g. prefix your app names)\nin order to enable multiple sites on the same server::\n\n    $ sudo port-for example.com/apache\n    35456\n\nPlease note that ``port-for`` script requires read and write access\nto ``/etc/port-for.conf``. This usually means regular users can read\nport values but sudo is required to associate a new port.\n\nList all associated ports::\n\n    $ port-for --list\n    foo: 37987\n    example.com/apache: 35456\n\nRemove an association::\n\n    $ sudo port-for --unbind foo\n    $ port-for --list\n    example.com/apache: 35456\n\n\nLibrary usage\n=============\n\n::\n\n    >>> import port_for\n    >>> port_for.select_random()\n    37774\n\n    >>> port_for.select_random()\n    48324\n\n    >>> 80 in port_for.available_good_ports()\n    False\n\n    >>> port_for.get_port()\n    34455\n\n    >>> port_for.get_port(\"1234\")\n    1234\n\n    >>> port_for.get_port((2000, 3000))\n    2345\n\n    >>> port_for.get_port({4001, 4003, 4005})\n    4005\n\n    >>> port_for.get_port([{4000, 4001}, (4100, 4200)])\n    4111\n\nDig into source code for more.\n\nContributing\n============\n\nDevelopment happens at github: https://github.com/kmike/port-for/\n\nIssue tracker: https://github.com/kmike/port-for/issues/new\n\nRelease\n=======\n\nInstall pipenv and --dev dependencies first, Then run:\n\n.. code-block::\n\n    pipenv run tbump [NEW_VERSION]\n",
    "bugtrack_url": null,
    "license": "Copyright (c) 2012 Mikhail Korobov  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Utility that helps with local TCP ports management. It can find an unused TCP localhost port and remember the association.",
    "version": "0.7.4",
    "project_urls": {
        "Bug Tracker": "https://github.com/kmike/port-for/issues",
        "Changelog": "https://github.com/kmike/port-for/blob/v0.7.4/CHANGES.rst",
        "Source": "https://github.com/kmike/port-for/"
    },
    "split_keywords": [
        "port",
        " posix"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9ca2579dcefbb0285b31f8d65b537f8a9932ed51319e0a3694e01b5bbc271f92",
                "md5": "22bb56b060c8c27f5af92a327f252d91",
                "sha256": "08404aa072651a53dcefe8d7a598ee8a1dca320d9ac44ac464da16ccf2a02c4a"
            },
            "downloads": -1,
            "filename": "port_for-0.7.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "22bb56b060c8c27f5af92a327f252d91",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 21369,
            "upload_time": "2024-10-09T12:28:37",
            "upload_time_iso_8601": "2024-10-09T12:28:37.853148Z",
            "url": "https://files.pythonhosted.org/packages/9c/a2/579dcefbb0285b31f8d65b537f8a9932ed51319e0a3694e01b5bbc271f92/port_for-0.7.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f684ad5114c85217426d7a5170a74a6f9d6b724df117c2f3b75e41fc9d6c6811",
                "md5": "f6fdaaf803d1bba1e4e0853d06e7f264",
                "sha256": "fc7713e7b22f89442f335ce12536653656e8f35146739eccaeff43d28436028d"
            },
            "downloads": -1,
            "filename": "port_for-0.7.4.tar.gz",
            "has_sig": false,
            "md5_digest": "f6fdaaf803d1bba1e4e0853d06e7f264",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 25077,
            "upload_time": "2024-10-09T12:28:38",
            "upload_time_iso_8601": "2024-10-09T12:28:38.875403Z",
            "url": "https://files.pythonhosted.org/packages/f6/84/ad5114c85217426d7a5170a74a6f9d6b724df117c2f3b75e41fc9d6c6811/port_for-0.7.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-09 12:28:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "kmike",
    "github_project": "port-for",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "port-for"
}
        
Elapsed time: 2.56365s