使用asyncio的upnp操作的python库
pyupnp-async的Python项目详细描述
使用asyncio的upnp操作的python库。
目前,这个库只为使用upnp进行端口转发提供了方便。对于其他upnp特性,它是 可能,但需要手动构造请求参数。(即soup请求xml。)
安装
要安装最新的开发版本,请运行:
git clone https://github.com/xinhuang/pyupnp-async.git cd pyupnp-async python setup.py install
此包不在pypi上。
快速教程
使用upnp创建端口转发:
frompyupnp_asyncimportmsearch_firstasyncdefforward_port(local_ip,local_port,ext_port,protocol):resp=awaitmsearch_first('urn:schemas-upnp-org:device:InternetGatewayDevice:1')device=awaitresp.get_device()service=device.find_first_service('urn:schemas-upnp-org:service:WANIPConnection:1')ext_ip=awaitservice.get_external_ip_address()try:awaitservice.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))exceptUpnpSoapErrorase:print(e)
使用upnp删除端口转发:
frompyupnp_asyncimportmsearch_firstasyncdefstop_forwarding(ext_port,protocol):resp=awaitmsearch_first('urn:schemas-upnp-org:device:InternetGatewayDevice:1')device=awaitresp.get_device()service=device.find_first_service('urn:schemas-upnp-org:service:WANIPConnection:1')ext_ip=awaitservice.get_external_ip_address()try:awaitservice.delete_port_mapping(ext_port,protocol)print('Data to external Port {} will not be forwarded any more.')exceptUpnpSoapErrorase:print(e)
API参考
msearch_first(search_target='upnp:rootdevice', max_wait=2, loop=None)
搜索指定的upnp目标,并返回第一个响应的目标。
Args:
- ^{tt2}$: Search target. For details please reference to UPnP spec.
- ^{tt3}$: Specify max waiting time in seconds.
- ^{tt4}$: Specify the event loop to be used. Default is ^{tt5}$
msearch_first(search_target='upnp:rootdevice', max_wait=2, loop=None)
搜索指定的upnp目标,并以异步迭代器的形式返回给定时间范围内响应的所有目标。
Args:
- ^{tt2}$: Search target. For details please reference to UPnP spec.
- ^{tt3}$: Specify max waiting time in seconds.
- ^{tt4}$: Specify the event loop to be used. Default is ^{tt5}$
许可
这个项目是根据麻省理工学院开放源码许可证的条款发布的。查看 license.txt了解更多信息。