Python SUDS错误

时间:2012-02-27 21:45:04

标签: python suds

我正在尝试将PHP脚本转换为python,但在我的生活中无法弄清楚为什么以下内容无效。

从SOAP服务查询返回的结果:

Suds ( https://fedorahosted.org/suds/ )  version: 0.4 GA  build: R699-20100913

Service ( ExternalQueryNameAvailabilityService ) tns="http://asic.gov.au/wsdl/name/availability/external"
Prefixes (6)
  ns0 = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
  ns1 = "http://www.w3.org/2005/05/xmlmime"
  ns2 = "uri:business.document.header.types.asic.gov.au"
  ns3 = "uri:external.query.name.availability.asic.gov.au"
  ns4 = "uri:fss.types.asic.gov.au"
  ns5 = "uri:types.asic.gov.au"
Ports (1):
  (ExternalQueryNameAvailabilityPort)
     Methods (1):
        externalQueryNameAvailability(ns2:businessDocumentHeaderType businessDocumentHeader, ns3:requestDataType businessDocumentBody, )
     Types (113):
        ns0:AttributedDateTime
        ns0:AttributedURI
        ns0:TimestampType
        ns5:abnApplicationReferenceNumberType
        ns5:abnType
        ns5:accountIdentifierType
        ns5:actionType
        ns5:addressType
        ns5:addressTypeType
        ns5:agentNameType
        ns5:agentType
        ns5:amountSignedType
        ns5:amountType
        ns5:applicationStatusType
        ns4:ascotDocumentNoType
        ns5:asicNumericIdType
        ns4:asicPaymentDetailsType
        ns5:asicSignatoryType
        ns2:attachmentType
        ns2:attachmentsType
        ns1:base64Binary
        ns5:birthDetailsType
        ns5:bnReferenceNumberType
        ns5:browserIdentifierType
        ns2:businessDocumentHeaderType
        ns2:businessDocumentRequestHeaderType
        ns5:businessNameIdentifierType
        ns5:codeType
        ns5:creditCardType
        ns4:customerReferenceNumberType
        ns4:debtorType
        ns5:descriptionType
        ns5:distinguishedNameType
        ns5:distinguishedWordType
        ns5:documentIdentifierType
        ns5:documentNoType
        ns5:emailType
        ns5:entityType
        ns5:exceptionListType
        ns5:exceptionType
        ns4:feeType
        ns4:feeWithAmountType
        ns4:feesType
        ns5:flagType
        ns4:fssAccountType
        ns4:fssCustomerType
        ns4:fssItemType
        ns4:fssSimpleAccountType
        ns4:fssTransactionType
        ns2:genericResultType
        ns1:hexBinary
        ns5:inboxIdentifierType
        ns5:intervalStatusType
        ns4:invoiceType
        ns5:itemSearchScopeType
        ns5:itemSummaryType
        ns5:itemTypeType
        ns5:keyType
        ns4:ledgerType
        ns5:lodgementIdentifierType
        ns2:messageEventType
        ns2:messageEventsType
        ns5:messageIdentifierType
        ns2:messageTimestampType
        ns2:messageTimestampsType
        ns5:nameAvailabilityType
        ns5:nameResultType
        ns5:nameResultWithObjectionsType
        ns5:nameType
        ns5:nniNameType
        ns5:nniNumberType
        ns5:objectionType
        ns5:organisationIdentifierType
        ns5:organisationNamePlusIdType
        ns5:originatingChannelType
        ns5:originatingServiceType
        ns5:outboundItemIdentifierType
        ns4:paymentDetailsType
        ns4:paymentMethodType
        ns5:paymentType
        ns5:personNameBirthType
        ns5:personNameType
        ns5:personNameWithRoleType
        ns4:priceType
        ns3:queryNameAvailabilityReplyType
        ns3:queryNameAvailabilityRequestType
        ns5:realmIdentifierType
        ns5:realmQualifierType
        ns4:receiptType
        ns5:referenceNoType
        ns5:rejectedType
        ns3:replyDataType
        ns5:replyType
        ns3:requestDataType
        ns5:requestFailedType
        ns5:requestRejectedType
        ns5:requestType
        ns5:resultType
        ns5:signatoryType
        ns5:soapSoftwareIdentifierType
        ns2:softwareInformationType
        ns5:standardHeaderType
        ns5:standardMessageHeaderType
        ns5:stateTerritoryCodeType
        ns5:statusType
        ns5:streetType
        ns4:suffixType
        ns0:tTimestampFault
        ns5:telephoneNumberType
        ns5:textType
        ns4:transactionType
        ns4:transactionsType
        ns5:trueType

尝试执行以下操作时:

con = connect('ExternalQueryNameAvailabilityPort', test, {'Content-Type': 'application/soap+xml'})
q_header = con.factory.create('ns2:businessDocumentHeaderType')
q_header.messageType = 'queryNameAvailability'
q_header.messageVersion = '2'
q_header.messageReferenceNumber = '100'
q_header.senderType = 'REGA'
q_header.senderId = '192'
q_body = con.factory.create('ns3:businessDocumentBody')
q_body.proposedName = 'Xtramedia.net PTY LTD'   
q_body.companyNameAvailabilityCheck = 'true'
q_body.bnNameAvailabilityCheck = 'true'
result = con.service.externalQueryNameAvailability(q_header, q_body)

我收到以下错误:

DEBUG:suds.client:http failed:
<?xml version='1.0' encoding='UTF-8'?>
  <S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
   <S:Body>
    <S:Fault xmlns:ns4="http://schemas.xmlsoap.org/soap/envelope/">
     <S:Code>
      <S:Value>S:Receiver</S:V6alue>
     </S:Code>
     <S:Reason>
      <S:Text xml:lang="en">org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'ns1:businessDocumentHeader'. One of '{"uri:business.document.header.types.asic.gov.au":businessDocumentHeader}' is expected
      </S:Text>
     </S:Reason>
    </S:Fault>
   </S:Body>
  </S:Envelope> 

为什么会抱怨这个元素的任何想法? - 我已经尝试删除元素声明中的“nsX”部分但同样的事情。

更新:以下是PHP脚本生成并成功的内容;

    <?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="uri:business.document.header.types.asic.gov.au" xmlns:ns2="uri:external.query.name.availability.asic.gov.au">
    <env:Body>
     <ns2:request>
      <ns1:businessDocumentHeader>
        <ns1:messageType>queryNameAvailability</ns1:messageType>
        <ns1:messageReferenceNumber>1</ns1:messageReferenceNumber>
        <ns1:messageVersion>2</ns1:messageVersion>
        <ns1:senderId>192</ns1:senderId>
        <ns1:senderType>REGA</ns1:senderType>
      </ns1:businessDocumentHeader>
      <ns2:businessDocumentBody>
        <ns2:proposedName>TEST</ns2:proposedName>
        <ns2:bnNameAvailabilityCheck>true</ns2:bnNameAvailabilityCheck>
      </ns2:businessDocumentBody>
    </ns2:request>
   </env:Body>
  </env:Envelope>

有人有任何想法吗?

更新2:我必须安装最新版本的SUDS才能实现这一目标。 - 非常感谢您的回答。

干杯, 本

2 个答案:

答案 0 :(得分:2)

更新(在PHP示例输出之后)

目前尚不清楚suds使用{uri:external.query.name.availability.asic.gov.au}的原因 <{1}}元素代替{uri:business.document.header.types.asic.gov.au}

快速而肮脏的解决方法是使用businessDocumentHeader例如:

suds.plugin

from suds.plugin import MessagePlugin

class NsHeaderPlugin(MessagePlugin):
    def sending(self, context):
        context.envelope = context.envelope.replace('ns1:businessDocumentHeader',
                                                    'ns0:businessDocumentHeader')

第二个参数应为class NsHeaderPlugin(MessagePlugin): def marshalled(self, context): hdr = context.envelope.childAtPath('Body/request/businessDocumentHeader') hdr.setPrefix('hdr', 'uri:business.document.header.types.asic.gov.au') ,而不是您指定的ns3:requestDataType

一般代码流程:

ns3:businessDocumentBody

我没有看到未定义的命名空间,因此在您的情况下似乎不需要from suds.client import Client # pip install suds #XXX: change envelope namespace from suds.bindings import binding binding.envns = (binding.envns[0], 'http://www.w3.org/2003/05/soap-envelope') del binding # change content type headers = {'Content-Type': 'application/soap+xml; charset="UTF-8"'} client = Client(wsdl_url, headers=headers, plugins=[NsHeaderPlugin()]) header = client.factory.create('{uri:business.document.header.types.asic.gov.au}' 'businessDocumentHeaderType') header.messageType = "queryNameAvailability" header.messageReferenceNumber = 1 header.messageVersion = 2 header.senderId = 192 header.senderType = "REGA" body = client.factory.create('{uri:external.query.name.availability.asic.gov.au}' 'requestDataType') body.proposedName = 'TEST' body.bnNameAvailabilityCheck = 'true' # make the call result = client.service.externalQueryNameAvailability(header, body) print result # for debugging, to find out what attributes are available 。但是,例如,如果使用ImportDoctor类型并且未提及xs:string,那么您可以修复wsdl架构:

'http://schemas.xmlsoap.org/soap/encoding/'

答案 1 :(得分:0)

错误几乎可以告诉您需要查看的位置:

      <S:Text xml:lang="en">org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'ns1:businessDocumentHeader'. One of '{"uri:business.document.header.types.asic.gov.au":businessDocumentHeader}' is expected

ns1引用网址http://www.w3.org/2005/05/xmlmime。如果在您的浏览器中访问它,您可以看到它没有多少。这进一步排除了ns1是正确的。

由于ns1:businessDocumentHeader甚至不在WSDL确定的类型列表中,因此您可能必须使用ImportDoctor。我在过去personal project of mine遇到类似问题,而WSDL没有正确导入类型。

对不起,这个回复并不是决定性的,但由于我看不到您的WSDL,因此很难确定确切的修复。尝试修改ImportDoctor的文档,看看是否有帮助。

尝试从这开始:

from suds.xsd.doctor import Import, ImportDoctor
imp = Import("http://www.w3.org/2005/05/xmlmime")
imp.filter.add("uri:business.document.header.types.asic.gov.au")
doctor = ImportDoctor(imp)

然后将doctor=doctor传递给客户端构造函数。