Skip to main content

A Python package for UPnP port forwarding

Project description

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)

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

upnp_forwarder-0.1.1.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

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

upnp_forwarder-0.1.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: upnp_forwarder-0.1.1.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for upnp_forwarder-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ef5c01a96a436e17033a4ac09f6c9249c4d3681beccef60406f4437a28e490f4
MD5 0473938637c332cdaae52956fd687469
BLAKE2b-256 abd2126859fdb00e3f3d5114f981ce3dbec178beebdbfd42c98692a20286427b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: upnp_forwarder-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for upnp_forwarder-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ffff86ce9b431ec1d9bcea533ef8c7240e337331e3d78b336946efe6d098461b
MD5 76764e1f06609aeef033f489cc3b94af
BLAKE2b-256 27b86b5aa18f79fbd7a92341384b27f2ab28b1669b53c1ac6888bb125e9b1b27

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