port-for


Nameport-for JSON
Version 0.7.2 PyPI version JSON
download
home_page
SummaryUtility that helps with local TCP ports management. It can find an unused TCP localhost port and remember the association.
upload_time2023-10-10 09:46:04
maintainer
docs_urlNone
author
requires_python>=3.8
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: PyPI Version

.. image:: http://codecov.io/github/kmike/port-for/coverage.svg?branch=master
   :target: http://codecov.io/github/kmike/port-for?branch=master
   :alt: 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.

.. 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": "",
    "name": "port-for",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "Grzegorz \u015aliwi\u0144ski <fizyk+pypi@fizyk.dev>",
    "keywords": "port,posix",
    "author": "",
    "author_email": "Mikhail Korobov <kmike84@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/69/d0/b843bcb330a2a6b90ce8599eea4a3a65863196bab9817d87ac1d9195679f/port-for-0.7.2.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: PyPI Version\n\n.. image:: http://codecov.io/github/kmike/port-for/coverage.svg?branch=master\n   :target: http://codecov.io/github/kmike/port-for?branch=master\n   :alt: Code Coverage\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.2",
    "project_urls": {
        "Bug Tracker": "https://github.com/kmike/port-for/issues",
        "Changelog": "https://github.com/kmike/port-for/blob/v0.7.2/CHANGES.rst",
        "Source": "https://github.com/kmike/port-for/"
    },
    "split_keywords": [
        "port",
        "posix"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3f3f9b6c24f1e2a05cabb2d7cf1e6675957707231d7d3bac1fb64ab463da0e17",
                "md5": "e04cd4d6514081932e82898b8304fd17",
                "sha256": "16b279ab4f210bad33515c45bd9af0c6e048ab24c3b6bbd9cfc7e451782617df"
            },
            "downloads": -1,
            "filename": "port_for-0.7.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e04cd4d6514081932e82898b8304fd17",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 21348,
            "upload_time": "2023-10-10T09:46:00",
            "upload_time_iso_8601": "2023-10-10T09:46:00.656692Z",
            "url": "https://files.pythonhosted.org/packages/3f/3f/9b6c24f1e2a05cabb2d7cf1e6675957707231d7d3bac1fb64ab463da0e17/port_for-0.7.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "69d0b843bcb330a2a6b90ce8599eea4a3a65863196bab9817d87ac1d9195679f",
                "md5": "e28eab0d79e7c272488e14f2f2eea453",
                "sha256": "074f29335130578aa42fef3726985e57d01c15189e509633a8a1b0b7f9226349"
            },
            "downloads": -1,
            "filename": "port-for-0.7.2.tar.gz",
            "has_sig": false,
            "md5_digest": "e28eab0d79e7c272488e14f2f2eea453",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 24948,
            "upload_time": "2023-10-10T09:46:04",
            "upload_time_iso_8601": "2023-10-10T09:46:04.484168Z",
            "url": "https://files.pythonhosted.org/packages/69/d0/b843bcb330a2a6b90ce8599eea4a3a65863196bab9817d87ac1d9195679f/port-for-0.7.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-10 09:46:04",
    "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: 0.12058s