2015-10-07 148 views
0

我正在嘗試使用WSDL和認證進行簡單的SOAP調用,但不知道如何將憑據傳遞給調用。這對於WSDL是可能的,還是我應該以其他方式做到這一點?SOAPpy WSDL客戶端認證

from SOAPpy import WSDL 

WSDLFILE = 'link/to/wsdl/file' 
server = WSDL.Proxy(WSDLFILE) 

result = server.methodCall() 

現在,我得到這個錯誤:

File "/usr/lib/pymodules/python2.7/SOAPpy/Client.py", line 263, in call 
raise HTTPError(code, msg) 
SOAPpy.Errors.HTTPError: <HTTPError 401 Unauthorized> 
+1

SOAPpy不贊成使用[suds](https://fedorahosted.org/suds/) - 我已經成功地使用過泡沫,查看他們的例子。 – Monkpit

+0

@Monkey謝謝,這兩個泡沫之間有着令人難以置信的差異,這是非常容易的。 – erhesto

+0

@Monkpit和@orhesto - 泡沫似乎被放棄了,雖然比起泡沫更新鮮,但是泡沫似乎也被拋棄了... – Greg0ry

回答

1

相當一個老問題,但由於谷歌搜索時,「SOAPpy的認證」首先顯示出來 - 我會離開我的發現,所以也許你贏了在10個小時內你不得不將頭撞到腦後。回饋社區。

谷歌搜索並沒有提供太多幫助,但在這裏,這個樣本(http://code.activestate.com/recipes/444758-how-to-add-cookiesheaders-to-soappy-calls/)讓我寫我自己的助手(版本0.0測試版)

from SOAPpy import (
    WSDL, 
    HTTPTransport, 
    Config, 
    SOAPAddress, 
) 
import urllib2 
import base64 

class AuthenticatedTransport(HTTPTransport): 
    _username = None 
    _password = None 

    def call(self, addr, data, namespace, soapaction = None, encoding = None, http_proxy = None, config = Config, timeout = 10): 
     if not isinstance(addr, SOAPAddress): 
      addr = SOAPAddress(addr, config) 

     t = 'text/xml'; 
     if encoding != None: 
      t += '; charset="%s"' % encoding 

     encoded_auth = None 
     if (isinstance(AuthenticatedTransport._username, str) != False): 
      if (isinstance(AuthenticatedTransport._password, str) == False): 
       AuthenticatedTransport._password = "" 
      encoded_auth = base64.b64encode('%s:%s' % (self._username, self._password)) 

     this_request = None 
     if (encoded_auth is not None): 
      this_request = urllib2.Request(
       url=address.proto + "://" + address.host + address.path, 
       data=data, 
       headers={ 
        "Content-Type":content_type, 
        "SOAPAction":"%s" % soapaction, 
        "Authorization":"Basic %s" % encoded_auth 
       } 
      ) 
     else: 
      this_request = urllib2.Request(
       url=address.proto + "://" + address.host + address.path, 
       data=data, 
       headers={ 
        "Content-Type":content_type, 
        "SOAPAction":"%s" % soapaction, 
       } 
      ) 

     response = urllib2.urlopen(this_request) 
     data = response.read() 

     # get the new namespace 
     if namespace is None: 
      new_ns = None 
     else: 
      new_ns = self.getNS(namespace, data) 

     # return response payload 
     return data, new_ns 

WSDL.Config.strictNamespaces = 0 

username = "your_username" 
password = "your_password" 

WSDLFile = "https://%s:%[email protected]_host/your_wsdl.php?wsdl" % (username, password) 
namespace = "http://futureware.biz/mantisconnect" 
proxy = WSDL.Proxy(WSDLFile, namespace=namespace, transport = AuthenticatedTransport(username,password)) 

print(proxy.mc_get_version()) # This is my WSDL call, your will be different 

無論出於何種原因,它是不夠用AuthenticatedTransport類的WSDL url本身也必須包含用戶名和密碼。也許是因爲WSDL調用的SOAP包裝正在創建單獨的HTTP會話(請記住在SOAPpy郵件列表上閱讀它)。

希望這有助於某人。