首页 > 解决方案 > 如何在 Python 中使用 zeep 设置默认 xmlns?

问题描述

我想使用 zeep 进行一些 API 调用。预期的输出是:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="https://cabapi-prod.service.dbrent.net/hal2_cabserver/">
  <soap:Body>
    <CABSERVER.listFreeBikes>
      <CommonParams>
        <UserData>
          <User>XXXXXXXXXXXXXX</User>
          <Password>XXXXXXXXXXXXXX</Password>
        </UserData>
        <LanguageUID>1</LanguageUID>
        <RequestTime>XXXXXXXXXXXXXX</RequestTime>
        <Version>2</Version>
      </CommonParams>
      <SearchPosition>
        <Longitude>XXXXXXXXXXXXXX</Longitude>
        <Latitude>XXXXXXXXXXXXXX</Latitude>
      </SearchPosition>
      <maxResults>100</maxResults>
      <searchRadius>844</searchRadius>
      <CustomerData>
        <Phone>XXXXXXXXXXXXXX</Phone>
        <Password>XXXXXXXXXXXXXX</Password>
        <PhoneUserEdited>false</PhoneUserEdited>
      </CustomerData>
    </CABSERVER.listFreeBikes>
  </soap:Body>
</soap:Envelope>

但是,我只能得到:

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
  <soap-env:Body>
    <ns0:CABSERVER.listFreeBikes xmlns:ns0="https://xml.dbcarsharing-buchung.de/hal2_cabserver/">
      <CommonParams>
        <UserData>
          <User>XXXXXXXXXXXXXX</User>
          <Password>XXXXXXXXXXXXXX</Password>
        </UserData>
        <LanguageUID>1</LanguageUID>
        <RequestTime>XXXXXXXXXXXXXX</RequestTime>
        <Version>2</Version>
      </CommonParams>
      <CustomerData>
        <Phone>XXXXXXXXXXXXXX</Phone>
        <Password>XXXXXXXXXXXXXX</Password>
        <PhoneUserEdited>false</PhoneUserEdited>
      </CustomerData>
      <SearchPosition>
        <Longitude>XXXXXXXXXXXXXX</Longitude>
        <Latitude>XXXXXXXXXXXXXX</Latitude>
      </SearchPosition>
      <maxResults>100</maxResults>
      <searchRadius>844</searchRadius>
    </ns0:CABSERVER.listFreeBikes>
  </soap-env:Body>
</soap-env:Envelope>

服务器无法正确识别我的请求,我认为这是因为命名空间。我做了一些研究,但没有找到将默认命名空间 (xmlns) 设置为https://xml.dbcarsharing-buchung.de/hal2_cabserver/的方法。相反,它被添加为额外的命名空间。

这是我当前的代码:

with client.settings(raw_response=True):
    ## Common Params related
    ### UserData related
    userNameType = client.get_type('ns0:Type_UserName')
    userPasswordType = client.get_type('ns0:Type_UserPassword')
    userName = userNameType('XXXXXXXXXXXXXX')
    userPassword = userPasswordType('XXXXXXXXXXXXXX')
    userDataType = client.get_type('ns0:Type_UserData')
    userData = userDataType(User = userName, Password = userPassword)


    current_time = time.localtime()
    current_time_str = time.strftime('%Y-%m-%dT%H:%M:%S.GMT+02:00', current_time)

    commonParamsType = client.get_type('ns0:Type_CommonParams')
    commonParams = commonParamsType(UserData = userData, LanguageUID = 1, RequestTime = current_time_str, Version = 2)

    ## CustomerData related
    customerPhoneType = client.get_type('ns0:Type_CustomerPhone')
    customerPhone = customerPhoneType('XXXXXXXXXXXXXX') # Get from config
    customerPasswordType = client.get_type('ns0:Type_CustomerPassword')
    customerPassword = customerPasswordType('XXXXXXXXXXXXXX') #Get from config
    customerDataType = client.get_type('ns0:Type_CustomerData')
    customerData = customerDataType(Phone = customerPhone, Password = customerPassword, PhoneUserEdited = False)

    ## SearchPosition related
    geoPositionType = client.get_type('ns0:Type_GeoPosition')
    geoPosition = geoPositionType(Longitude = XXXXXXXXXXXXXX, Latitude = XXXXXXXXXXXXXX)

    # DEBUG
    node = client.create_message(client.service, 'CABSERVER.listFreeBikes', CommonParams = commonParams, CustomerData = customerData, SearchPosition = geoPosition, maxResults = 100, searchRadius = 844)
    from lxml import etree as ET
    tree = ET.ElementTree(node)
    tree.write('test.xml', pretty_print=True)

到目前为止我尝试了什么:

client.set_ns_prefix('ns0', '')

但是,这并不能解决我的问题。我的想法不多了。谁能指出我正确的方向?

可以在此处找到 WSDL:https ://xml.dbcarsharing-buchung.de/hal2_cabserver/definitions/HAL2_CABSERVER_3.wsdl

标签: pythonpython-3.xsoapzeep

解决方案


你很亲近

client.set_ns_prefix('ns0', '')

应该读

client.set_ns_prefix(None, "https://xml.dbcarsharing-buchung.de/hal2_cabserver/")

然后应该使用默认命名空间。


推荐阅读