ephemeral-port-reserve

raw JSON →
1.1.4 verified Fri May 01 auth: no python

A small Python library that binds to an ephemeral port, forces it into the TIME_WAIT state, and then unbinds, making the port reserved for use by the caller without interference. Current version 1.1.4, maintenance mode with no active development.

pip install ephemeral-port-reserve
error ImportError: No module named ephemeral_port_reserve
cause The library is not installed or the import path is incorrect.
fix
Run 'pip install ephemeral-port-reserve' (note hyphens) and import with underscores: 'from ephemeral_port_reserve import reserve'.
error OSError: [Errno 98] Address already in use
cause The reserved port is still in TIME_WAIT from a previous use, or another process is using it.
fix
The library avoids this by using time_wait state, but if it occurs, wait for the TIME_WAIT to expire (typically 2 * MSL) or use a different port. Retry reservation.
gotcha The reserved port is only guaranteed to be available for the lifetime of the calling process. Once the process exits, the port may be reused by the OS or another process.
fix Ensure your process holds the reservation for the duration needed. Do not rely on cross-process persistence.
gotcha On some systems (e.g., Linux), the reservation may fail if the system is under heavy load or if ephemeral port range is exhausted. The function raises an exception in such cases.
fix Handle exceptions from reserve() and implement retry logic or fallback mechanisms.
deprecated The library is in maintenance mode and is not actively developed. No new features or bug fixes are expected. Consider alternative approaches for advanced port management.
fix If you need more robust port reservation, consider using low-level socket APIs or platform-specific solutions.

Reserve an ephemeral port using the reserve() function. Port is automatically released on process exit.

from ephemeral_port_reserve import reserve

# Reserve an ephemeral port
port = reserve()
print(f"Reserved port: {port}")
# Use the port...
# No explicit release needed; the reservation vanishes when the process exits.