如何通过SOAP上传文件,使用python zeep作为附件

时间:2017-05-20 06:49:29

标签: python soap upload attachment zeep

我在调用(远程)方法和下载附件时成功使用了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:<someContentID>,其中<someContentID>是附件的内容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--

1 个答案:

答案 0 :(得分:0)

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

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

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

有关