肥皂泡IP控制出口设备

2024-06-25 05:56:46 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能用soap exportDevice调用从IPControl获取数据吗?在

我就是这么做的。它仍然返回Context ContextId_1428068970573 is null,必须先调用initExportDevice。在

import suds
import logging

def test_ipcontrol():

    logging.basicConfig(level=logging.INFO)
    logging.getLogger('suds.client').setLevel(logging.DEBUG)
    #url="https://ipcontrol.dhl.com/inc-ws/services/Exports?wsdl"

    url="http://prgdca-lab-ipcmgr01.dhl.com:8080/inc-ws/services/Exports?wsdl"
    client = suds.client.Client(url, username="srv_dnsauto-test", password="jFlri$mbYto3ot9q", timeout=360)
    rslt = client.service.initExportDevice(filter="name begins 'czhs0876'")

    print client.last_received()

    from suds.sax.element import Element

    sid = Element('SessionId', ns=('ns1','http://xml.apache.org/axis/session')).setText(client.last_received().getChild('soapenv:Envelope').getChild('soapenv:Header').getChild('ns1:sessionID').getText())
    sid.set("SOAP-ENV:mustUnderstand", "0")
    sid.set("SOAP-ENV:actor", "http://schemas.xmlsoap.org/soap/actor/next")
    sid.set("xsi:type", "soapenc:long")

    client.set_options(soapheaders=sid)
    rslt2 = client.service.exportDevice(context=rslt)
    print rslt2

Tags: testimportclienthttpurlloggingservicesoap
1条回答
网友
1楼 · 发布于 2024-06-25 05:56:46

我不知道你有没有得到这个问题的答案,但我也遇到了同样的问题,经过几天的努力,我刚刚解决了。我不得不从多个地方借用,包括上面你自己的代码,但最后它是有效的。在

一个问题是您需要从client.last_received()中提取的会话ID丢失。我必须使用在别处找到的MessagePlugin代码来获取原始XML,然后使用正则表达式来提取会话ID。接下来,我使用了您的大部分代码来构建新的SOAP头,但是使用了'sessionID'而不是{}。一旦我把这些东西都准备好了,它就起作用了。下面是一些初始化并执行exportChildBlock()的基本查询代码。在

import re
import sys
from suds.plugin import MessagePlugin
from suds.transport.http import HttpAuthenticated
from suds.client import Client
from suds.sax.element import Element
from creds import user, pw


class MyPlugin(MessagePlugin):
    def __init__(self):
        self.last_sent_raw = None
        self.last_received_raw = None

    def sending(self, context):
        self.last_sent_raw = str(context.envelope)

    def received(self, context):
        self.last_received_raw = str(context.reply)


def main():

    # Build transport and client objects using HttpAuth for authentication
    # and MessagePlugin to allow the capturing of raw XML, necessary for
    # extracting the IPControl session ID later

    plugin = MyPlugin()
    credentials = dict(username=user, password=pw)
    transport = HttpAuthenticated(**credentials)
    client = Client(
        'http://ipcontrol.foobar.net/inc-ws/services/Exports?wsdl',
        headers={'username':user, 'password':pw},
        transport=transport,
        plugins=[plugin]
        )

    query_parameters = ('ipaddress = 1.2.3.4', False)  # The boolean is for includeFreeBlocks

    # Exports must be initialized first. The IPControl session ID can then
    # be extracted from the raw reply data. This is necessary later when
    # the export service is called.

    context = client.service.initExportChildBlock(*query_parameters)
    session_id_re = re.search(r'([0-9-]{19,20})', str(plugin.last_received_raw), re.M)
    if session_id_re:
        session_id = session_id_re.group(1)
        print("SESSION ID: {}".format(session_id))
    else:
        print("NO SESSION ID FOUND")
        sys.exit()

    # Build a new SOAP header where the 'sessionID' is set to the value extracted
    # from the raw initialization reply data above, then apply the new
    # header to the existing client object.

    sid = Element('sessionID', ns=('ns1', 'http://xml.apache.org/axis/session')).setText(session_id)
    sid.set("SOAP-ENV:mustUnderstand", "0")
    sid.set("SOAP-ENV:actor", "http://schemas.xmlsoap.org/soap/actor/next")
    sid.set("xsi:type", "soapenc:long")
    client.set_options(soapheaders=sid)

    result = client.service.exportChildBlock(context)
    print("RESULT: {}".format(result))

if __name__ == '__main__':
    main()

相关问题 更多 >