Portend

3.2.1 · active · verified Mon Apr 13

Portend is a Python library for TCP port monitoring and discovery. It provides routines to wait for a port to become free or occupied, check the current state of a port, or identify a suitable port available for binding locally. The library is currently at version 3.2.1 and is actively maintained with a stable release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `portend.occupied` to wait for a port to become active and `portend.free` to wait for a port to be released. It includes a simple simulation of a server binding and unbinding a port.

import portend
import socket
import time

def demo_port_usage():
    # Example: Wait for an occupied port (e.g., a simple server)
    # Start a dummy server in a separate thread/process for this to work
    # For demonstration, we'll simulate a server binding/unbinding

    # Find a free port
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(('localhost', 0))
    port = s.getsockname()[1]
    s.close()
    print(f"Found a free port: {port}")

    # Simulate a process binding to the port
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind(('localhost', port))
    server_socket.listen(1)
    print(f"Server bound to localhost:{port}")

    # Use portend to wait for the port to be occupied
    print(f"Waiting for localhost:{port} to be occupied (max 5s)...")
    try:
        portend.occupied('localhost', port, timeout=5)
        print(f"Success: localhost:{port} is occupied.")
    except portend.Timeout: # Removed the direct import for Timeout as it's an attribute of portend module and was causing an error.
        print(f"Error: Timeout waiting for localhost:{port} to be occupied.")
    finally:
        server_socket.close()
        print(f"Server unbound from localhost:{port}")

    # Use portend to wait for the port to be free
    print(f"Waiting for localhost:{port} to be free (max 5s)...")
    try:
        portend.free('localhost', port, timeout=5)
        print(f"Success: localhost:{port} is free.")
    except portend.Timeout:
        print(f"Error: Timeout waiting for localhost:{port} to be free.")

demo_port_usage()

view raw JSON →