apppublic/test/test_aioupnp.py

32 lines
760 B
Python
Raw Normal View History

2022-02-28 12:06:25 +08:00
import asyncio
from aioupnp.upnp import UPnP
async def main():
upnp = await UPnP.discover()
print(dir(upnp))
print('gateway=', upnp.gateway, upnp.gateway_address, upnp.lan_address)
print(await upnp.get_external_ip())
2022-02-28 14:01:18 +08:00
port = 40009
while port < 41000:
x = await upnp.get_specific_port_mapping(40009, 'TCP')
if len(x) == 0:
print(port, 'port available')
break
else:
print(port, 'port occupied')
port += 1
2022-02-28 12:06:25 +08:00
print("adding a port mapping")
2022-02-28 14:01:18 +08:00
x = await upnp.add_port_mapping(port, 'TCP', 8999, '192.168.1.8', 'test mapping')
print(8999, '-->', port)
2022-02-28 12:06:25 +08:00
print('x=', x, await upnp.get_redirects())
# print("deleting the port mapping")
# await upnp.delete_port_mapping(51234, 'TCP')
2022-02-28 14:01:18 +08:00
# print(await upnp.get_redirects())
2022-02-28 12:06:25 +08:00
asyncio.run(main())