Skip to main content

pyupnp-async

A Python library for UPnP operations using asyncio.

Currently this library only provided convenience for port forwarding using UPnP. For other UPnP features, it is possible, but manual construction of request parameters is needed. (I.E. the SOUP request XML.)

Installation

To install the latest development version, run:

git clone https://github.com/xinhuang/pyupnp-async.git
cd pyupnp-async
python setup.py install

This package is not on pypi.

Quick Tutorial

Create a port forwarding using UPnP:

from pyupnp_async import msearch_first

async def forward_port(local_ip, local_port, ext_port, protocol):
    resp = await msearch_first('urn:schemas-upnp-org:device:InternetGatewayDevice:1')
    device = await resp.get_device()
    service = device.find_first_service('urn:schemas-upnp-org:service:WANIPConnection:1')
    ext_ip = await service.get_external_ip_address()
    try:
        await service.add_port_mapping(local_port, ext_port, local_ip, protocol)
        print('Data to external Port {} will be forwarded to {}:{}'.format(ext_port, local_ip, local_port))
    except UpnpSoapError as e:
        print(e)

Delete a port forwarding using UPnP:

from pyupnp_async import msearch_first

async def stop_forwarding(ext_port, protocol):
    resp = await msearch_first('urn:schemas-upnp-org:device:InternetGatewayDevice:1')
    device = await resp.get_device()
    service = device.find_first_service('urn:schemas-upnp-org:service:WANIPConnection:1')
    ext_ip = await service.get_external_ip_address()
    try:
        await service.delete_port_mapping(ext_port, protocol)
        print('Data to external Port {} will not be forwarded any more.')
    except UpnpSoapError as e:
        print(e)

API Reference

msearch_first(search_target='upnp:rootdevice', max_wait=2, loop=None)

Searches for UPnP target specified, and returns the first responsed target.

Args:
  • search_target: Search target. For details please reference to UPnP spec.

  • max_wait: Specify max waiting time in seconds.

  • loop: Specify the event loop to be used. Default is asyncio.get_event_loop()

msearch_first(search_target='upnp:rootdevice', max_wait=2, loop=None)

Searches for UPnP target specified, and returns all targets responded in given time range as an async iterator.

Args:
  • search_target: Search target. For details please reference to UPnP spec.

  • max_wait: Specify max waiting time in seconds.

  • loop: Specify the event loop to be used. Default is asyncio.get_event_loop()

Licensing

This project is released under the terms of the MIT Open Source License. View LICENSE.txt for more information.

Metadata

Release files for pyupnp-async 0.1.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 pyupnp-async 0.1.1.1
File Size Uploaded
pyupnp-async-0.1.1.1.tar.gz 6.4 kB Details

Release files / pyupnp-async-0.1.1.1.tar.gz

Download URL pyupnp-async-0.1.1.1.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a384818c809841f9efeb8d9e4578599c33efc8675c357d22b9eb3cbe8867d793
BLAKE2b-256 checksum
How to use checksums
b1a40f734fb63c26fb449ec647a0b1e251aacdc37d6aba599bc54601d04d38d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.19.9 CPython/3.6.5

Release history Release notifications | RSS feed

This release

0.1.1.1 This release

1 release file

0.1.0.2

1 release file

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