Skip to main content

upnp_forwarder

A Python package for easily adding and removing UPnP port mappings on your router.

Installation

You can install the package using pip:

pip install upnp_forwarder

Usage

The package provides two main functions: add_port_mapping and delete_port_mapping.

add_port_mapping

Adds a UPnP port mapping.

from upnp_forwarder import add_port_mapping, UPnPError
import logging

# Optional: Configure logging to see package output
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

local_port = 8000
external_port = 8000
protocol = 'TCP' # 'TCP' or 'UDP'
description = 'My Application Port'
lease_duration = 3600 # Lease duration in seconds (0 for infinite)

try:
    if add_port_mapping(local_port, external_port, protocol, description, lease_duration):
        print(f"Successfully added port mapping: External Port {external_port} -> Local Port {local_port}")
    else:
        print("Failed to add port mapping.")
except UPnPError as e:
    print(f"UPnP Error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

Parameters:

  • local_port (int): The local port on your machine to forward.
  • external_port (int): The external port on your router to open.
  • protocol (str): The protocol to use ('TCP' or 'UDP').
  • description (str): A description for the port mapping.
  • lease_duration (int, optional): The duration in seconds for the mapping to be active. Defaults to 3600 seconds. Use 0 for an infinite lease (if supported by the router).

delete_port_mapping

Deletes a UPnP port mapping.

from upnp_forwarder import delete_port_mapping, UPnPError
import logging

# Optional: Configure logging to see package output
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

external_port = 8000
protocol = 'TCP' # 'TCP' or 'UDP'

try:
    if delete_port_mapping(external_port, protocol):
        print(f"Successfully deleted port mapping: External Port {external_port}")
    else:
        print(f"Failed to delete port mapping: External Port {external_port}")
except UPnPError as e:
    print(f"UPnP Error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

Parameters:

  • external_port (int): The external port of the mapping to delete.
  • protocol (str): The protocol of the mapping to delete ('TCP' or 'UDP').

Error Handling

The package may raise upnp_forwarder.UPnPError for UPnP-specific issues (e.g., no UPnP devices found, failed to select IGD, failed to add/delete mapping). It's recommended to wrap calls in a try...except UPnPError block to handle these cases.

Contributing

(Optional section: Add information on how others can contribute to your project)

License

This project is licensed under the MIT License. (Or your chosen license)

Metadata

Release files for upnp-forwarder 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for upnp-forwarder 0.1.1
File Size Uploaded Provenance
upnp_forwarder-0.1.1.tar.gz 3.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for upnp-forwarder 0.1.1
File Interpreter ABI Platform
upnp_forwarder-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 7.2 kB

Release files / upnp_forwarder-0.1.1.tar.gz

Download URL upnp_forwarder-0.1.1.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ef5c01a96a436e17033a4ac09f6c9249c4d3681beccef60406f4437a28e490f4
BLAKE2b-256 checksum
How to use checksums
abd2126859fdb00e3f3d5114f981ce3dbec178beebdbfd42c98692a20286427b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.11

Release files / upnp_forwarder-0.1.1-py3-none-any.whl

Download URL upnp_forwarder-0.1.1-py3-none-any.whl
Size 3.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ffff86ce9b431ec1d9bcea533ef8c7240e337331e3d78b336946efe6d098461b
BLAKE2b-256 checksum
How to use checksums
27b86b5aa18f79fbd7a92341384b27f2ab28b1669b53c1ac6888bb125e9b1b27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.11

Security for upnp-forwarder 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page