2013-03-22 145 views
3

我對於REST請求使用了python requests模塊。使用Python請求模塊發出SOAP請求

我試圖做一個SOAP請求,但我不知道不能得到一個這樣的例子。 這是我的肥皂身體和標題。

<auth> 
<apikey>xcvzxcvcxzv-a0-0035c6fbc04f</apikey> 
</auth> 

<reports> 
<report> 
<name>Test</name> 
</report> 
</reports> 

這裏是WSDL URL

https://ltn.net/webservices/booking/r1/index.wsdl 

請告訴我,我怎樣才能使POST請求在這裏使用python。如果使用requests模塊是不可能的,那麼其他選擇可能是什麼?

+0

你有沒有嘗試專門的SOAP模塊,如'suds'? – wRAR 2013-03-22 11:54:55

+0

不,我打算使用睡眠或請求或httplib2 – masterofdestiny 2013-03-22 11:57:02

+0

然後,您需要構建請求並手動解析響應。 – wRAR 2013-03-22 11:58:33

回答

6

要與SOAP服務器工作,你應該使用專業圖書館。不幸的是,沒有用於Python的SOAP客戶端模塊,我使用的最好的模塊是suds

2

,因爲我不知道任何人實際上已實施,但沫支持使得suds.transport.Transport的定製實現這在很大程度上是假設。一旦這樣是suds.transport.http.HttpTransport。因此,從技術上講,通過實施傳輸子類,您可以創建使用請求的泡沫傳輸。

http://jortel.fedorapeople.org/suds/doc/suds.transport.http.HttpTransport-class.html是什麼默認使用,並返回 http://jortel.fedorapeople.org/suds/doc/suds.transport.Reply-class.html所以你可以看到,它應該是相當簡單的包裝要求回覆,使泡沫理解他們。交通運輸的要求(應與請求發送)在這裏是 http://jortel.fedorapeople.org/suds/doc/suds.transport.Request-class.html 我可能遲早實現這一點,如果沒有一個甜菜我給它

爲什麼這一切的努力呢?因爲suds在內部使用urllib2,遠不如python-requests作爲HTTP客戶端實現。

但是多了一個編輯:我提供作爲起始點https://gist.github.com/nanonyme/6268358一個要點。這是麻省理工學院的代碼,未經測試,但應作爲交通工具的起點。

+0

現在有一個[suds_requests](https://pypi.python.org/pypi/suds_requests)項目就是這樣做的。它提供了一個請求運輸的泡沫(或suds-jurko)庫。 – 2016-04-29 16:11:32

7

我遇到了同樣的問題,最近,不幸的是沒有泡沫也不jurko - 泡沫(泡沫的保持叉)能夠提供幫助。 這主要是因爲泡沫保持產生誤格式化SOAP信封(這尤其會發生,如果是應該要生成的肥皂有一些內容應該是一個CDATA內)從服務器期待什麼不同。即使當我嘗試使用__inject選項自己注入肥皂信封時,情況也是如此。

這裏是我如何解決它使用python請求

import requests 

request = u"""<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://SOME_URL"> 
    <soapenv:Header> 
     <user>{0}</user> 
     <pass>{1}</pass> 
    </soapenv:Header> 
    <soapenv:Body> 
     <req:RequestInfo><![CDATA[<?xml version='1.0' encoding='UTF-8'?><request xmlns='http://SOME_OTHER_URL'> 
    <Call> 
     <callID>{2}</callID> 
     ... 
    </Call> 
    <Foo> 
     <Bar> 
      <Baz>{3}</Baz> 
      ... 
     </Bar> 
     <Neytri> 
      ... 
     </Neytri> 
    </Foo> 
    <Title>{4}</Title> 
</request>]]></req:RequestInfo> 
    </soapenv:Body> 
</soapenv:Envelope>""".format('Jake_Sully', 
           'super_secret_pandora_password', 
           'TYSGW-Wwhw', 
           'something_cool', 
           'SalaryPayment', 
           'Pandora_title', 
          ) 

encoded_request = request.encode('utf-8') 

headers = {"Host": "http://SOME_URL", 
      "Content-Type": "application/soap+xml; charset=UTF-8", 
      "Content-Length": str(len(encoded_request)), 
      "SOAPAction": "http://SOME_OTHER_URL"} 

response = requests.post(url="http://SOME_OTHER_URL", 
        headers = headers, 
        data = encoded_request, 
        verify=False) 

print response.content #print response.text 

什麼是真正重要的是要指定報頭中的Content-Type和也將SOAPAction。 按照SOAP 1.1 specifiaction

The SOAPAction HTTP request header field can be used to indicate the intent of the SOAP HTTP request. The value is a URI identifying the intent. SOAP places no restrictions on the format or specificity of the URI or that it is resolvable. An HTTP client MUST use this header field when issuing a SOAP HTTP Request.  

的SOAPAction的值通常可以在你想的API調用的WSDL文件中找到;如果從WSDL文件不存在,那麼你可以使用一個空字符串作爲標題

的值另請參見:

  1. http://archive.oreilly.com/pub/post/unraveling_the_mystery_of_soap.html
  2. http://www.prodigyproductionsllc.com/articles/programming/call-a-webservice-using-soap-and-python/
+4

如果您需要發佈到運行WSDL的IIS,只需將Content-Type標題更改爲「Content-Type」:「text/xml; charset = utf-8」。 – Genome 2015-04-23 00:34:21

+1

如果發佈到salesforce,請將** SOAPAction **值設置爲「POST」以完成請求。 – abhi 2016-07-13 18:34:46