Skip to main content

Python Library for UPnP operations using asyncio

Project description

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.

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

pyupnp-async-0.1.1.1.tar.gz (6.4 kB view details)

Uploaded Source

File details

Details for the file pyupnp-async-0.1.1.1.tar.gz.

File metadata

  • Download URL: pyupnp-async-0.1.1.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for pyupnp-async-0.1.1.1.tar.gz
Algorithm Hash digest
SHA256 a384818c809841f9efeb8d9e4578599c33efc8675c357d22b9eb3cbe8867d793
MD5 ce895a970381b00d4e11eb2c8dd62909
BLAKE2b-256 b1a40f734fb63c26fb449ec647a0b1e251aacdc37d6aba599bc54601d04d38d2

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