Skip to main content

Reserve and manage localhost ports; atomically update .env and config.json

Project description

PortKeeper

Reserve and manage localhost hosts/ports for starting servers. Transparently updates .env and config.json files and keeps a local registry so multiple processes and users can coordinate port reservations.

Features

  • Reserve a free port (optionally with preferred port or a search range)
  • Optionally hold the port by binding a dummy socket (prevents others from taking it)
  • Release reservation
  • Atomic updates to .env and config.json (with backup)
  • Simple file locking to avoid races
  • Context manager API and a tiny CLI (portkeeper)

Quickstart

pip install portkeeper

# Reserve preferred 8888 or a port in 8888-8988, hold it, and print JSON
portkeeper reserve --preferred 8888 --range 8888 8988 --hold --owner myapp

# From Python
from portkeeper import PortRegistry
with PortRegistry().reserve(preferred=8888, port_range=(8888, 8988), hold=True) as r:
    PortRegistry().write_env({'PORT': str(r.port)})

CLI

  • reserve [--preferred P] [--range START END] [--hold] [--owner OWNER] [--write-env KEY]
  • release PORT
  • status (list reserved ports in registry)
  • gc (garbage-collect stale registry entries)

Author

Tom Sapletta
🏢 Organization: softreck
🌐 Website: softreck.com

Tom Sapletta is a software engineer and the founder of softreck, specializing in system automation, DevOps tools, and infrastructure management solutions. With extensive experience in Python development and distributed systems, Tom focuses on creating tools that simplify complex development workflows.

Professional Background

  • Expertise: System Architecture, DevOps, Python Development
  • Focus Areas: Port Management, Infrastructure Automation, Development Tools
  • Open Source: Committed to building reliable, well-tested tools for the developer community

Publishing to PyPI

To publish the PortKeeper package to PyPI, you need to set up your credentials. You can do this in one of two ways:

Option 1: Environment Variables (Recommended)

Set the following environment variables in your shell configuration (e.g., .bashrc, .zshrc) or in your CI/CD environment:

export TWINE_USERNAME="__token__"
export TWINE_PASSWORD="pypi-<YOUR_TOKEN>"

Replace <YOUR_TOKEN> with your actual PyPI API token. You can generate a token in your PyPI account settings under "API tokens".

Option 2: Configuration File

Alternatively, you can configure your credentials in a ~/.pypirc file:

[distutils]
index-servers =
  pypi
  testpypi

[pypi]
username = __token__
password = pypi-<YOUR_TOKEN>

[testpypi]
username = __token__
password = pypi-<YOUR_TEST_TOKEN>

Publishing Commands

Once your credentials are set up, you can use the following Makefile commands:

  • make build - Build the package (wheel and sdist)
  • make publish - Build and upload to PyPI
  • make publish-test - Build and upload to TestPyPI
  • make release-patch - Bump patch version, build, and publish to PyPI
  • make release-minor - Bump minor version, build, and publish to PyPI
  • make release-major - Bump major version, build, and publish to PyPI
  • make sign-artifacts - Sign distribution artifacts with GPG (if available)

Note: Make sure to test your package on TestPyPI before publishing to the main PyPI repository.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2025 Tom Sapletta

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

portkeeper-0.1.1.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

portkeeper-0.1.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file portkeeper-0.1.1.tar.gz.

File metadata

  • Download URL: portkeeper-0.1.1.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for portkeeper-0.1.1.tar.gz
Algorithm Hash digest
SHA256 16ee83c21f0af2e592e8461968206b526a13499714c95c696aeffc039b28bd77
MD5 e271b70e231849be6daf846660dcd976
BLAKE2b-256 8f30853f71d2ddb0ec990934c07b465aa6c2aea33bff33afa1ee27b05bf0c44b

See more details on using hashes here.

File details

Details for the file portkeeper-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: portkeeper-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for portkeeper-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7f0782d70f489a93704821d554e9a29afc28f8691305ecd1be70abbd86898bfb
MD5 2e9fa33579e8b0fa2cb43c125bc5f9c7
BLAKE2b-256 8fa61bbab9a75a74c2b4f30851934a19d544eae2d5899dc4716c62c0371ba0c9

See more details on using hashes here.

Supported by

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