aioupnp/README.md

134 lines
4.6 KiB
Markdown
Raw Normal View History

2018-10-19 16:11:13 +02:00
[![Build Status](https://travis-ci.org/lbryio/aioupnp.svg?branch=master)](https://travis-ci.org/lbryio/aioupnp)
2018-10-08 07:55:42 +02:00
[![codecov](https://codecov.io/gh/lbryio/aioupnp/branch/master/graph/badge.svg)](https://codecov.io/gh/lbryio/aioupnp)
2018-10-18 17:53:10 +02:00
[![PyPI version](https://badge.fury.io/py/aioupnp.svg)](https://badge.fury.io/py/aioupnp)
2018-10-19 16:11:13 +02:00
[![Python 3.6](https://img.shields.io/badge/python-3.6-blue.svg)](https://www.python.org/downloads/release/python-360/)
[![Python 3.7](https://img.shields.io/badge/python-3.7-blue.svg)](https://www.python.org/downloads/release/python-370/)
2019-10-25 20:50:33 +02:00
[![Python 3.8](https://img.shields.io/badge/python-3.8-blue.svg)](https://www.python.org/downloads/release/python-380/)
2018-09-26 06:16:02 +02:00
2018-10-08 04:30:13 +02:00
# UPnP for asyncio
2018-07-30 23:48:20 +02:00
2020-01-16 21:34:02 +01:00
`aioupnp` is a python 3.6-8 library and command line tool to interact with UPnP gateways using asyncio. `aioupnp` requires the `netifaces` and `defusedxml` modules.
2018-07-30 23:48:20 +02:00
2018-10-12 19:56:15 +02:00
## Supported devices
2020-10-04 07:19:43 +02:00
![img](https://i.imgur.com/JtO4glP.png)
2018-07-30 23:48:20 +02:00
2019-11-15 16:46:15 +01:00
## Installation
2018-10-16 15:01:25 +02:00
2019-11-15 16:46:15 +01:00
Verify python is version 3.6-8
2018-10-16 15:01:25 +02:00
```
python --version
```
2020-10-29 03:11:48 +01:00
#### Installation for normal usage
2018-10-12 19:56:15 +02:00
```
pip install aioupnp
```
2020-10-29 03:11:48 +01:00
#### Installation for development
2018-07-30 23:51:59 +02:00
```
2018-10-12 19:56:15 +02:00
git clone https://github.com/lbryio/aioupnp.git
cd aioupnp
pip install -e .
2018-07-30 23:51:59 +02:00
```
2018-07-30 23:48:20 +02:00
2018-10-12 19:56:15 +02:00
2018-07-30 23:48:20 +02:00
## Usage
2018-07-31 17:35:24 +02:00
```
2018-10-12 19:56:15 +02:00
aioupnp [-h] [--debug_logging] [--interface=<interface>] [--gateway_address=<gateway_address>]
2018-10-08 07:00:42 +02:00
[--lan_address=<lan_address>] [--timeout=<timeout>]
2018-10-12 19:56:15 +02:00
[(--<case sensitive m-search header>=<value>)...]
2018-10-08 07:00:42 +02:00
command [--<arg name>=<arg>]...
```
2018-10-12 19:56:15 +02:00
#### Commands
* `help`
* `get_external_ip`
* `m_search`
* `add_port_mapping`
* `get_port_mapping_by_index`
* `get_redirects`
* `get_specific_port_mapping`
* `delete_port_mapping`
* `get_next_mapping`
* `gather_debug_info`
2020-10-04 07:19:43 +02:00
#### To get the documentation for a command
2018-07-30 23:52:16 +02:00
aioupnp help get_external_ip
2018-07-30 23:48:20 +02:00
#### To get the external ip address
2018-07-30 23:52:16 +02:00
2018-10-08 23:57:45 +02:00
aioupnp get_external_ip
2019-01-22 22:22:38 +01:00
#### To list the active port mappings on the gateway
2018-10-08 23:57:45 +02:00
aioupnp get_redirects
2019-01-22 22:30:06 +01:00
#### To set up a TCP port mapping
aioupnp add_port_mapping --external_port=1234 --internal_port=1234 --lan_address=<lan_addr> --description=test --protocol=TCP
2018-10-08 23:57:45 +02:00
#### To delete a TCP port mapping
2018-10-08 23:57:45 +02:00
aioupnp delete_port_mapping --external_port=1234 --protocol=TCP
2018-10-08 23:57:45 +02:00
#### M-Search headers
2020-10-14 21:08:21 +02:00
UPnP uses a multicast protocol (SSDP) to locate the gateway. Gateway discovery is automatic by default, but you may provide specific headers for the search to use to override automatic discovery.
2018-10-08 23:57:45 +02:00
If m-search headers are provided as keyword arguments then all of the headers to be used must be provided, in the order they are to be used. For example:
aioupnp --HOST=239.255.255.250:1900 --MAN=\"ssdp:discover\" --MX=1 --ST=upnp:rootdevice m_search
#### Using non-default network interfaces
By default, the network device will be automatically discovered. The interface may instead be specified with the `--interface`, provided before the command to be run. The gateway used on the interface network may be specified with the `--gateway_address` argument.
aioupnp --interface=wlp4s0 --gateway_address=192.168.1.6 m_search
2020-10-13 23:05:14 +02:00
#### Example usage from python
from aioupnp.upnp import UPnP
async def main():
upnp = await UPnP.discover()
print(await upnp.get_external_ip())
print(await upnp.get_redirects())
print("adding a port mapping")
await upnp.add_port_mapping(1234, 'TCP', 1234, upnp.lan_address, 'test mapping')
print(await upnp.get_redirects())
print("deleting the port mapping")
await upnp.delete_port_mapping(1234, 'TCP')
print(await upnp.get_redirects())
asyncio.run(main())
## Troubleshooting
#### Debug logging
2020-10-29 03:11:48 +01:00
To enable verbose debug logging, add the `--debug_logging` argument before the command being run
aioupnp --debug_logging m_search
2020-10-29 03:11:48 +01:00
#### Is it turned on?
Check that UPnP is turned on in the web gui for your router.
#### It really doesn't work
2020-10-29 03:11:48 +01:00
If it always fails with an m-search error, or the UPnP device is found but making a port mapping or getting the external address fails, a bug report can be generated and automatically sent using the `generate_bug_report.py` script. This script will run a packet capture while attempting to find the device and add/remove a mapping using `miniupnpc` and `aioupnp`. Once complete, it will submit a bug report of the packets sent/recieved by aioupnp/miniupnpc.
To run the bug report script, first `pip install certifi aiohttp miniupnpc`. You'll also need `aioupnp` installed. Then generate and send the bug report with `sudo /full/path/to/your/python generate_bug_report.py`.
2018-10-08 23:57:45 +02:00
2018-07-30 23:48:20 +02:00
## License
This project is MIT licensed. For the full license, see [LICENSE](LICENSE).
## Contact
2019-11-15 16:46:15 +01:00
The primary contact for this project is [@jackrobison](mailto:jackrobison@lbry.com)