如何在Python中使用zeep设置默认xmlns?

时间:2019-04-23 13:42:27

标签: python python-3.x soap zeep

我想使用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

1 个答案:

答案 0 :(得分:0)

你很亲密

client.set_ns_prefix('ns0', '')

应阅读

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

然后应使用默认名称空间。