Python bindings for the MiniUPnP library, used to control UPnP-enabled Internet Gateway Devices (IGD) for port forwarding and NAT traversal. Version 2.3.3 is the latest release; updates are infrequent (months to years between releases).
pip install miniupnpcVerified import paths — ran on the pinned version, not inferred.
Discover UPnP device, get external IP, add and remove a TCP port mapping.
Use `upnp.addportmapping(port, 'TCP', internal_ip, internal_port, desc, '')` — last argument must be a string.
Use `upnp.getgenericportmapping(index)` for numbered mappings or `upnp.getspecificportmappingentry(external_port, protocol)` for a specific rule.
After `discover()`, check `if num > 0:` before calling `selectigd()`, otherwise it may raise a runtime error or silently fail.
Call `deleteportmapping()` or use a `try/finally` block to remove mappings when done.
Check the return code of `addportmapping()` (0 = success, non-zero = failure) and log it. Use diagnostic tools like `upnpc -a` from the command line if available.
Upgrade to Python 3.5+.
Always provide a description string, even if empty. Use `upnp.addportmapping(port, proto, int_ip, int_port, 'mapping')`.
Use `from miniupnpc import UPnP` instead of importing the module and accessing the attribute.
Install the library via `pip install miniupnpc` (or `python -m pip install miniupnpc` if pip is not in PATH).
Ensure indices are integers, not strings. For example, use `for i in range(upnp.getportmappingnumberofentries()):` then `upnp.getgenericportmapping(i)`.
Verify you are on a network with a UPnP-enabled router. Try running the `upnpc` command-line tool from the miniupnpc package to confirm discovery works.