在Zeep中使用OnVif PullPoint Services

时间:2018-12-31 16:33:34

标签: soap onvif zeep python-onvif

我很难在标准IP摄像机中订阅OnVif Pullpoint服务。

我正在使用的SOAP客户端是Zeep https://python-zeep.readthedocs.io/en/master/index.html

Zeep似乎构造了错误的xml数据,但是我可能是错的(由于我对SOAP的了解有限)。让我们来看一个例子:

from zeep.client import Client, CachingClient, Settings
from zeep.wsse.username import UsernameToken
import zeep.helpers

import logging.config

# # Put Zeep into verbose mode
logging.config.dictConfig({
    'version': 1,
    'formatters': {
        'verbose': {
            'format': '%(name)s: %(message)s'
        }
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        },
    },
    'loggers': {
        'zeep.transports': {
            'level': 'DEBUG',
            'propagate': True,
            'handlers': ['console'],
        },
    }
})

ip="192.168.0.134"; user="admin"; passwd="123456"; port=80 # My home cam 1.  Now you know its username and password.  :)

settings = Settings()
settings.strict = False
settings.xml_huge_tree = True

# # WSDL File
url = "https://www.onvif.org/ver10/events/wsdl/event.wsdl"

# # *** Events Service ***
xaddr = "http://"+ip+"/onvif/events_service"
print("creating a soap client with url = ", url)
zeep_client_events = CachingClient(wsdl=url, wsse=UsernameToken(user, passwd, use_digest=True), settings=settings)
print("soap client created")
print("binding to service")
ws_client_events = zeep_client_events.create_service("{http://www.onvif.org/ver10/events/wsdl}EventBinding", xaddr)
print("service OK")

# # *** PullPoint Service ***
xaddr = "http://"+ip+"/onvif/events_service"
print("creating a soap client with url = ", url)
zeep_client_pp = CachingClient(wsdl=url, wsse=UsernameToken(user, passwd, use_digest=True), settings=settings)
print("soap client created")
print("binding to service")
ws_client_pp = zeep_client_pp.create_service("{http://www.onvif.org/ver10/events/wsdl}PullPointSubscriptionBinding", xaddr)
print("service bound")

res = ws_client_events.CreatePullPointSubscription()

# # could see the namespaces like this:
# zeep_client_pp.namespaces

# # could create PullMessages' parameters like this:
# pm = zeep_client_pp.get_element("ns7:PullMessages")()

# So, this call never works
ws_client_pp.PullMessages(MessageLimit=1, Timeout="PT1S")

根据相机的不同,这总是导致“远端封闭连接无响应”,否则服务器将发送一条消息,指出该值无效。

将Zeep置于详细模式并检查SOAP消息正文(也已通过Wireshark确认)时,它看起来像这样:

<soap-env:Body>
    <ns0:PullMessages xmlns:ns0="http://www.onvif.org/ver10/events/wsdl">
        <ns0:Timeout>P%P</ns0:Timeout>
        <ns0:MessageLimit>1</ns0:MessageLimit>
    </ns0:PullMessages>
</soap-env:Body>

因此,似乎字符串“ PT1S”没有进入消息正文,但是仍然存在“ P%P”代替!

如何说服Zeep将正确的时间插入到位?

P。 S。而且,请不要告诉我使用“ python-onvif-zeep”。当然,我首先这样做,然后最终遇到了这个问题(pullpoint服务的“ python-onvif-zeep”示例不起作用)

2 个答案:

答案 0 :(得分:0)

必须属于 isodate.Duration 类。这样可以解决问题:

import isodate
Timeout = isodate.Duration(seconds=10)

答案 1 :(得分:0)

请注意,events.wsdl中使用的日期时间似乎是XML类型。在Python中,datedel从datetime开始支持。

import datetime
timeout = datetime.timedelta(seconds=100)

ws_client_pp.PullMessages(MessageLimit=1, Timeout=timeout)

以上应用于zeep,生成正确的请求。 100 s是PT1M40S。

我无论如何都无法继续使用相机,但现在的时长已正确设置。