Lightweight UPnP client library for Python.
Project description
UPnPy
Lightweight UPnP client library for Python.
Examples
Get the external IP address of an Internet Gateway Device:
import upnpy
upnp = upnpy.UPnP()
upnp.discover() # Discover UPnP devices on the network
upnp.select_igd() # Select the IGD
# Select the service which implements the "GetExternalIPAddress" action
# Some routers don't implement WANIPConnection but WANPPPConnection instead:
# urn:schemas-upnp-org:service:WANPPPConnection:1
upnp.select_service('urn:schemas-upnp-org:service:WANIPConnection:1')
# Execute the "GetExternalIPAddress" action
upnp.execute('GetExternalIPAddress')
If the above code executed successfully, it should return a dictionary containing the external IP Address:
{'NewExternalIPAddress': 'xxx.xxx.xxx.xxx'}
Add a new port mapping to an Internet Gateway Device:
import upnpy
upnp = upnpy.UPnP()
upnp.discover() # Discover UPnP devices on the network
upnp.select_igd() # Select the IGD
# Select the service which implements the "AddPortMapping" action
# Some routers don't implement WANIPConnection but WANPPPConnection instead:
# urn:schemas-upnp-org:service:WANPPPConnection:1
upnp.select_service('urn:schemas-upnp-org:service:WANIPConnection:1')
# Execute the "AddPortMapping" action
upnp.execute('AddPortMapping', 80, 'TCP', 8000, '192.168.1.3', 'Test UPnPy entry')
If the above code executed successfully, a new port mapping should've been created on your router.
80
is the port that will be opened on the WAN side of the router
TCP
is the transport protocol you want to use (can also be UDP
)
8000
is the port of the internal client where a server will be listening
192.168.1.3
is the IP of the internal client where connections should be forwarded to
Test UPnPy entry
is a description of the port mapping.
Returns an empty dictionary:
{}
Documentation
Documentation is available at https://upnpy.readthedocs.io
License
This project is licensed under the terms of the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.