2012-09-02 217 views
2

我想用SOAP協議查詢Internet中的服務。當爲Python搜索SOAP庫時,這篇文章提供了信息:https://stackoverflow.com/a/206964。但是我沒有嘗試過這個特定服務的庫。我有PHP腳本的工作原理:python肥皂客戶端庫

<?php 

$client = new SoapClient('https://personyze.com/site/service/service/social_archive/', array('trace' => true)); 
$result = $client->__soapCall 
( 'select', array 
    ( array('server_id'=>123456, 'api_key'=>123456), 'user_id', null, null, false, 0, 1 
    ) 
); 
echo $client->__getLastRequestHeaders(), $client->__getLastRequest(), "RESULT:\n"; 
var_dump($result); 

我已經做了以下嘗試做同樣在Python:

1.

from suds.client import Client 
client = Client("https://personyze.com/site/service/service/social_archive/") 

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.7/dist-packages/suds-0.3.7-py2.7.egg/suds/client.py", line 109, in __init__ 
    self.wsdl = Definitions(url, options) 
    File "/usr/local/lib/python2.7/dist-packages/suds-0.3.7-py2.7.egg/suds/wsdl.py", line 194, in __init__ 
    self.build_schema() 
    File "/usr/local/lib/python2.7/dist-packages/suds-0.3.7-py2.7.egg/suds/wsdl.py", line 255, in build_schema 
    self.schema = container.load() 
    File "/usr/local/lib/python2.7/dist-packages/suds-0.3.7-py2.7.egg/suds/xsd/schema.py", line 92, in load 
    child.dereference() 
    File "/usr/local/lib/python2.7/dist-packages/suds-0.3.7-py2.7.egg/suds/xsd/schema.py", line 295, in dereference 
    midx, deps = x.dependencies() 
    File "/usr/local/lib/python2.7/dist-packages/suds-0.3.7-py2.7.egg/suds/xsd/sxbasic.py", line 330, in dependencies 
    raise TypeNotFound(self.ref) 
suds.TypeNotFound: Type not found: '(Array, http://schemas.xmlsoap.org/soap/encoding/,)' 

2.

from pysimplesoap.client import SoapClient 
client = SoapClient(wsdl="https://personyze.com/site/service/service/social_archive/", trace=True) 
client.select({"server_id":123456, "api_key":123456}, "user_id") 

很多更好!今天最好的結果。這個庫唯一錯誤的是它調用方法selectRequest()而不是select()

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.7/dist-packages/pysimplesoap/client.py", line 140, in <lambda> 
    return lambda *args, **kwargs: self.wsdl_call(attr,*args,**kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/pysimplesoap/client.py", line 289, in wsdl_call 
    response = self.call(method, *params) 
    File "/usr/local/lib/python2.7/dist-packages/pysimplesoap/client.py", line 188, in call 
    raise SoapFault(unicode(response.faultcode), unicode(response.faultstring)) 
pysimplesoap.client.SoapFault: SOAP-ENV:Server: Procedure 'selectRequest' not present 

3.

import SOAPpy 
client = SOAPpy.SOAPProxy("https://personyze.com/site/service/service/social_archive/", "urn:SocialArchiveServiceProviderwsdl") 
client.select({"server_id":123456, "api_key":123456}, "user_id", None, None, False, 0, 2) 

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.7/dist-packages/SOAPpy-0.12.6-py2.7.egg/SOAPpy/Client.py", line 540, in __call__ 
    return self.__r_call(*args, **kw) 
    File "/usr/local/lib/python2.7/dist-packages/SOAPpy-0.12.6-py2.7.egg/SOAPpy/Client.py", line 562, in __r_call 
    self.__hd, self.__ma) 
    File "/usr/local/lib/python2.7/dist-packages/SOAPpy-0.12.6-py2.7.egg/SOAPpy/Client.py", line 464, in __call 
    p, attrs = parseSOAPRPC(r, attrs = 1) 
    File "/usr/local/lib/python2.7/dist-packages/SOAPpy-0.12.6-py2.7.egg/SOAPpy/Parser.py", line 1074, in parseSOAPRPC 
    t = _parseSOAP(xml_str, rules = rules) 
    File "/usr/local/lib/python2.7/dist-packages/SOAPpy-0.12.6-py2.7.egg/SOAPpy/Parser.py", line 1054, in _parseSOAP 
    parser.parse(inpsrc) 
    File "/usr/lib/python2.7/xml/sax/expatreader.py", line 107, in parse 
    xmlreader.IncrementalParser.parse(self, source) 
    File "/usr/lib/python2.7/xml/sax/xmlreader.py", line 123, in parse 
    self.feed(buffer) 
    File "/usr/lib/python2.7/xml/sax/expatreader.py", line 207, in feed 
    self._parser.Parse(data, isFinal) 
    File "/usr/lib/python2.7/xml/sax/expatreader.py", line 338, in start_element_ns 
    AttributesNSImpl(newattrs, qnames)) 
    File "/usr/local/lib/python2.7/dist-packages/SOAPpy-0.12.6-py2.7.egg/SOAPpy/Parser.py", line 109, in startElementNS 
    "got `%s'" % toStr(name) 
SOAPpy.Errors.Error: <Error : expected `SOAP-ENV:Envelope', got `wsdl:definitions'> 

請告訴我,我能做些什麼,以使其發揮作用。

回答

2

服務器的WSDL模式似乎被破壞 - 它引用了命名對象而不導入它們。

請參閱suds documentation on fixing broken schemas關於如何解決泡沫問題。

另請參閱this question瞭解更多詳情。

這似乎爲我工作:

from suds.xsd.doctor import Import 
from suds.xsd.doctor import ImportDoctor 
from suds.client import Client 


url = 'https://personyze.com/site/service/service/social_archive/' 
tns = 'urn:SocialArchiveServiceProviderwsdl' 

imp = Import('http://schemas.xmlsoap.org/soap/encoding/', 'http://schemas.xmlsoap.org/soap/encoding/') 
imp.filter.add(tns) 
client = Client(url,plugins=[ImportDoctor(imp)]) 

print client.service.select({"server_id":123456, "api_key":123456}, "user_id") 
+0

架構是真正打破。該服務使用YII框架來生成模式,在使用數組時無法生成有效的模式。 – jeremiah