如何向生成的请求插入元素?
How insert element to generated request?
我使用 onvif wsdls。
我想用过滤器发送请求 "Subscribe"。
过滤器包含 TopicExpression。像这样请求:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:b="http://docs.oasis-open.org/wsn/b-2"
xmlns:add="http://www.w3.org/2005/08/addressing">
<soap:Header/>
<soap:Body>
<b:Subscribe>
<b:ConsumerReference>
<add:Address>http://10.1.50.11:8000</add:Address>
<!--Optional:-->
<add:ReferenceParameters>
<!--You may enter ANY elements at this point-->
</add:ReferenceParameters>
<!--Optional:-->
<add:Metadata>
<!--You may enter ANY elements at this point-->
</add:Metadata>
<!--You may enter ANY elements at this point-->
</b:ConsumerReference>
<!--Optional:-->
<b:Filter>
<!--You may enter ANY elements at this point-->
<wsnt:TopicExpression Dialect="http://www.onvif.org/ver10/tev/topicExpression/ConcreteSet" xmlns:o2kvmd="http://www.o2kvmd.com/onvif-sdk/v1" xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2" xmlns:tns1="http://www.onvif.org/ver10/topics">o2kvmd:Tele//.</wsnt:TopicExpression>
</b:Filter>
<!--Optional:-->
<!--type: AbsoluteOrRelativeTimeType-->
<b:InitialTerminationTime>PT60S</b:InitialTerminationTime>
<!--Optional:-->
<b:SubscriptionPolicy>
<!--You may enter ANY elements at this point-->
</b:SubscriptionPolicy>
<!--You may enter ANY elements at this point-->
</b:Subscribe>
在 python 代码中,我可以在没有过滤器的情况下发送请求。代码如下:
notification = mycam.create_notification_service()
subscribe = notification.create_type('Subscribe')
obj = subscribe(ConsumerReference='http://10.1.50.11:8000',InitialTerminationTime='PT60S', Filter={})
notification.Subscribe(obj)
请求发送成功
但我不知道如何像上面的请求那样将 TopicExpression 插入到 Filter 中。
请告诉我,我该怎么做?
我发现这是一个很好的解决方案:
topic_expression_type = zeep_client.get_type('TopicExpressionType')
from zeep import xsd
topic_exp = xsd.Element('{http://docs.oasis-open.org/wsn/b-2}TopicExpression',xsd.ComplexType(topic_expression_type))
subscribe = notification.create_type('Subscribe')
any = xsd.AnyObject(topic_exp,topic_expression_type(_value_1=xsd.AnyObject(xsd.String(), 'o2kvmd:Telemetry//.'),Dialect='http://www.onvif.org/ver10/tev/topicExpression/ConcreteSet'))
obj = subscribe(ConsumerReference='http://10.1.50.11:8000', InitialTerminationTime='PT60S', Filter={'_value_1': any})
notification.Subscribe(obj)
我使用 onvif wsdls。
我想用过滤器发送请求 "Subscribe"。 过滤器包含 TopicExpression。像这样请求:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:b="http://docs.oasis-open.org/wsn/b-2"
xmlns:add="http://www.w3.org/2005/08/addressing">
<soap:Header/>
<soap:Body>
<b:Subscribe>
<b:ConsumerReference>
<add:Address>http://10.1.50.11:8000</add:Address>
<!--Optional:-->
<add:ReferenceParameters>
<!--You may enter ANY elements at this point-->
</add:ReferenceParameters>
<!--Optional:-->
<add:Metadata>
<!--You may enter ANY elements at this point-->
</add:Metadata>
<!--You may enter ANY elements at this point-->
</b:ConsumerReference>
<!--Optional:-->
<b:Filter>
<!--You may enter ANY elements at this point-->
<wsnt:TopicExpression Dialect="http://www.onvif.org/ver10/tev/topicExpression/ConcreteSet" xmlns:o2kvmd="http://www.o2kvmd.com/onvif-sdk/v1" xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2" xmlns:tns1="http://www.onvif.org/ver10/topics">o2kvmd:Tele//.</wsnt:TopicExpression>
</b:Filter>
<!--Optional:-->
<!--type: AbsoluteOrRelativeTimeType-->
<b:InitialTerminationTime>PT60S</b:InitialTerminationTime>
<!--Optional:-->
<b:SubscriptionPolicy>
<!--You may enter ANY elements at this point-->
</b:SubscriptionPolicy>
<!--You may enter ANY elements at this point-->
</b:Subscribe>
在 python 代码中,我可以在没有过滤器的情况下发送请求。代码如下:
notification = mycam.create_notification_service()
subscribe = notification.create_type('Subscribe')
obj = subscribe(ConsumerReference='http://10.1.50.11:8000',InitialTerminationTime='PT60S', Filter={})
notification.Subscribe(obj)
请求发送成功
但我不知道如何像上面的请求那样将 TopicExpression 插入到 Filter 中。 请告诉我,我该怎么做?
我发现这是一个很好的解决方案:
topic_expression_type = zeep_client.get_type('TopicExpressionType')
from zeep import xsd
topic_exp = xsd.Element('{http://docs.oasis-open.org/wsn/b-2}TopicExpression',xsd.ComplexType(topic_expression_type))
subscribe = notification.create_type('Subscribe')
any = xsd.AnyObject(topic_exp,topic_expression_type(_value_1=xsd.AnyObject(xsd.String(), 'o2kvmd:Telemetry//.'),Dialect='http://www.onvif.org/ver10/tev/topicExpression/ConcreteSet'))
obj = subscribe(ConsumerReference='http://10.1.50.11:8000', InitialTerminationTime='PT60S', Filter={'_value_1': any})
notification.Subscribe(obj)