python的轻量级upnp客户端库。

UPn的Python项目详细描述


upnpy

Build StatusPyPI - Python VersionPyPI package versionMIT License

python的轻量级upnp客户端库。

示例

获取Internet Gateway Device

的外部IP地址
>>>importupnpy>>>upnp=upnpy.UPnP()>>>>>>devices=upnp.discover()# Discover UPnP devices on the network>>>devices[Device<BroadcomADSLRouter>]>>>>>>device=upnp.get_igd()# Select the IGD>>># Or alternatively>>>device=devices[0]>>>>>>device.get_services()# Get the services available for this device[<Service(Layer3Forwarding)id="Layer3Forwarding.1">,<Service(WANCommonInterfaceConfig)id="WANCommonInterfaceConfig.1">,<Service(WANPPPConnection)id="WANPPPConnection.1">]>>>>>># We can now access a specific service on the device by its ID>>># The IDs for the services in this case contain illegal values so we can't access it by an attribute>>># so we access it like a dictionary instead.>>>device['WANPPPConnection.1']<Service(WANPPPConnection)id="WANPPPConnection.1">>>># Get the actions available for the service>>>device['WANPPPConnection.1'].get_actions()[<Actionname="SetConnectionType">,<Actionname="GetConnectionTypeInfo">,<Actionname="RequestConnection">,<Actionname="ForceTermination">,<Actionname="GetStatusInfo">,<Actionname="GetNATRSIPStatus">,<Actionname="GetGenericPortMappingEntry">,<Actionname="GetSpecificPortMappingEntry">,<Actionname="AddPortMapping">,<Actionname="DeletePortMapping">,<Actionname="GetExternalIPAddress">]>>>device['WANPPPConnection.1'].GetExternalIPAddress()# Finally, get the external IP address{'NewExternalIPAddress':'xxx.xxx.xxx.xxx'}

将新端口映射添加到Internet Gateway Device

>>>importupnpy>>>upnp=upnpy.UPnP()>>>>>>devices=upnp.discover()# Discover UPnP devices on the network>>>devices[Device<BroadcomADSLRouter>]>>>>>>device=upnp.get_igd()# Select the IGD>>># Or alternatively>>>device=devices[0]>>>>>>device.get_services()# Get the services available for this device[<Service(Layer3Forwarding)id="Layer3Forwarding.1">,<Service(WANCommonInterfaceConfig)id="WANCommonInterfaceConfig.1">,<Service(WANPPPConnection)id="WANPPPConnection.1">]>>>>>># We can now access a specific service on the device by its ID>>># The IDs for the services in this case contain illegal values so we can't access it by an attribute>>># so we access it like a dictionary instead.>>>device['WANPPPConnection.1']<Service(WANPPPConnection)id="WANPPPConnection.1">>>># Get the actions available for the service>>>device['WANPPPConnection.1'].get_actions()[<Actionname="SetConnectionType">,<Actionname="GetConnectionTypeInfo">,<Actionname="RequestConnection">,<Actionname="ForceTermination">,<Actionname="GetStatusInfo">,<Actionname="GetNATRSIPStatus">,<Actionname="GetGenericPortMappingEntry">,<Actionname="GetSpecificPortMappingEntry">,<Actionname="AddPortMapping">,<Actionname="DeletePortMapping">,<Actionname="GetExternalIPAddress">]>>>device['WANPPPConnection.1'].AddPortMapping<Actionname="AddPortMapping">>>>>>># Lets see what arguments the action accepts>>># (use the get_output_arguments() method for a list of output arguments)>>>device['WANPPPConnection.1'].AddPortMapping.get_input_arguments()[{'name':'NewRemoteHost','data_type':'string','allowed_value_list':[]},{'name':'NewExternalPort','data_type':'ui2','allowed_value_list':[]},{'name':'NewProtocol','data_type':'string','allowed_value_list':['TCP','UDP']},{'name':'NewInternalPort','data_type':'ui2','allowed_value_list':[]},{'name':'NewInternalClient','data_type':'string','allowed_value_list':[]},{'name':'NewEnabled','data_type':'boolean','allowed_value_list':[]},{'name':'NewPortMappingDescription','data_type':'string','allowed_value_list':[]},{'name':'NewLeaseDuration','data_type':'ui4','allowed_value_list':[]}]>>>>>># Yikes, lets pretty print that :)>>>importjson>>>print(json.dumps(device['WANPPPConnection.1'].AddPortMapping.get_input_arguments(),indent=4))[{"name":"NewRemoteHost","data_type":"string","allowed_value_list":[]},{"name":"NewExternalPort","data_type":"ui2","allowed_value_list":[]},{"name":"NewProtocol","data_type":"string","allowed_value_list":["TCP","UDP"]},{"name":"NewInternalPort","data_type":"ui2","allowed_value_list":[]},{"name":"NewInternalClient","data_type":"string","allowed_value_list":[]},{"name":"NewEnabled","data_type":"boolean","allowed_value_list":[]},{"name":"NewPortMappingDescription","data_type":"string","allowed_value_list":[]},{"name":"NewLeaseDuration","data_type":"ui4","allowed_value_list":[]}]>>># Finally, add the new port mapping to the IGD>>>device['WANPPPConnection.1'].AddPortMapping(...NewRemoteHost='',...NewExternalPort=80,...NewProtocol='TCP',...NewInternalPort=8000,...NewInternalClient='192.168.1.3',...NewEnabled=1,...NewPortMappingDescription='Test port mapping entry from UPnPy.',...NewLeaseDuration=0...){}

文档

文档可在https://upnpy.readthedocs.io/en/latest/

许可证

这个项目是根据MIT License的条款授权的。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java有没有工具可以将zephyr转换为velocity模板?   java在安卓 studio中从JSON响应中获取值   jvm如何在Java中设计一个好的permgen空间字符串?   java如何防止Rest webservice使用被盗令牌进行身份验证   java无法遍历列表JSTL   找不到用于ResourceServerTokenServices的java Bean SpringSecurityOauth2   java子字符串替换问题   爪哇玻璃鱼3。十、 以编程方式处理任意HTTPSession的终止   java如何检查输入是否为整数,并在最后添加一个命令来重新启动while循环?   引发java ical4j 1.0.6不可解析日期异常   Java等价于Delphi的DBCtrlGrid?   如果发生错误,java将查找下一个预期标记ANTLR 3   java自打开应用程序(创建锁屏)   java为什么netty有自己的ConcurrentHashMap?   Gradle任务中的java拉取和运行依赖项   继承与Java继承的混淆   java使用shell脚本中的版本执行jar   java我无法让Sqlite数据库与带有Maven的JavaFX应用程序IDE Eclipse包正确通信   java控制台日志未通过org打印。阿帕奇。hadoop。mapreduce。作业的waitForCompletion(true)方法   JAVAlang.NoSuchMethodError:apachestorm螺栓中的spring getrequest