【问题标题】:How to upload a file via SOAP, as an attachment using python zeep如何通过 SOAP 上传文件,作为使用 python zeep 的附件
【发布时间】:2017-05-20 06:49:28
【问题描述】:

我在调用(远程)方法和下载附件时已经成功使用了zeep。

我现在遇到了一种需要我上传文件的方法。该文件需要作为附件传递。我通常如下调用远程Web服务方法:

client.service.fooMethod(arg1,arg2,...)

在我的特殊情况下,arg1 是一个 URI,一个我希望上传到服务器的文件。它需要作为附件上传。 我该怎么做?

这是一个更具体的例子:

方法名称为 UploadPortfolios,具有以下架构:

<xs:element name="UploadPortfolio">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="tns:URI"/>
      <xs:element ref="tns:PortfolioID"/>
      <xs:element ref="tns:AsOfDate"/>
      <xs:element minOccurs="0" ref="tns:SuppressPositionLog"/>
      <xs:element minOccurs="0" ref="tns:PositionDetailLogAsAttachment"/>
      <xs:element minOccurs="0" ref="tns:UploadSharedPortfolio"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

我在 python 中使用 zeep 来调用 UploadPortfolios。 uri 参数需要包含字符串cid:&lt;someContentID&gt;,其中&lt;someContentID&gt; 是附件的内容ID:

portfolio_management_wsdl = 'https://ondemand.uat.riskmetrics.com/ondemand/soap/PortfolioManagement?wsdl'
client_pfm = Client(portfolio_management_wsdl, transport=transport, wsse=wsse)
uri = r'cid://SomeDataFile.xml'
args = {'URI':uri, 'AsOfDate':'20160129'}
result = client_pfm.service.UploadPortfolios(**args)

不用说上面的行不通,因为,不知何故我需要发送附件。

使用 SoapUI (https://www.soapui.org/downloads/soapui.html) 我可以毫无问题地调用该函数。以下是SoapUI生成并发送到服务器的原始数据(部分已省略)

POST https://ondemand.uat.riskmetrics.com/ondemand/soap/PortfolioManagement.PortfolioManagementHttp    sSoap12Endpoint/ HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: multipart/related; type="application/xop+xml"; start="<rootpart@soapui.org>"; start-info="application/soap+xml"; action="urn:RiskMetricsWS:1.0:PortfolioManagement:UploadPortfolio"; boundary="----=_Part_46_453204030.1495210657807"
MIME-Version: 1.0
Content-Length: 7668
Host: ondemand.uat.riskmetrics.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)


------=_Part_46_453204030.1495210657807
Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml"; action="UploadPortfolio"
Content-Transfer-Encoding: 8bit
Content-ID: <rootpart@soapui.org>

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:wsdl="http://..." xmlns:xsd="http://..." xmlns:xsd1="http://...">
   <soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" ... </soap:Header>
   <soap:Body>
      <wsdl:UploadPortfolio>
         <wsdl:URI><inc:Include href="cid:530345234005" xmlns:inc="http://www.w3.org/2004/08/xop/include"/></wsdl:URI>
         <wsdl:PortfolioID>TestPtf_RML4</wsdl:PortfolioID>
         <wsdl:AsOfDate>20170509</wsdl:AsOfDate>
         <wsdl:SuppressPositionLog>false</wsdl:SuppressPositionLog>
         <wsdl:PositionDetailLogAsAttachment>true</wsdl:PositionDetailLogAsAttachment>
         <wsdl:UploadSharedPortfolio>true</wsdl:UploadSharedPortfolio>
      </wsdl:UploadPortfolio>
   </soap:Body>
</soap:Envelope>
------=_Part_46_453204030.1495210657807
Content-Type: text/xml; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-ID: <530345234005>
Content-Disposition: attachment; name="SomeDataFile.xml"

<someInformation>
...
</someInformation>

------=_Part_46_453204030.1495210657807--

【问题讨论】:

    标签: python soap upload attachment zeep


    【解决方案1】:

    我尝试使用由 ellethee 创建的 transport_with_attach,并将 client.attach(filename) 作为 URI 的参数传递。它看起来很有希望,并且消息具有正确的形式。但是服务器响应:

     javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
     Message: Premature end of file.
    

    这与格式错误的 xml 有关(我认为)

    【讨论】:

    猜你喜欢
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多