开发者

add header in request SOAP message in python

I try to add header when sending SOAP request in Python.

The header in SOAP is:

> <SOAP-ENV:Header> 
> <ns3:userCredentials
> xsi:type="https://4psa.com/HeaderData.xsd/2.0.0">
> <username>admin</username>  
> <password>welcome</password> 
> </ns3:userCredentials>
> </SOAP-ENV:Header>

I have used:

 from suds.client import Client
 from suds.xsd.doctor import ImportDoctor, Import
 wsdl = 'https://192.168.1.15//soap2/schema/2.5.0/Report/Report.wsdl'
 client = Client(wsdl)

and I don`t know how to add header to this code.

Please suggest how to add it.

And I tried:

> >>> from suds.client import Client
> >>> from suds.xsd.doctor import ImportDoctor, Import
> >>> imp = Import('http://schemas.xmlsoap.org/soap/encoding/')
> >>> url = 'https://192.168.1.15//soap2/schema/2.5.0/Report/Report.wsdl'
> >>> client = Client(url)
> >>> userid = 'admin'
> >>> passwd = '12345678@X'
> >>> client.set_options(soapheaders=(userid,passwd))
> >>> print client a get error when run:
> 
> >>> client.service.CallCosts(1) Traceback (most recent call last):  
> File "<stdin>", line 1, in <module>  
> File
> "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/client.py",
> line 542, in __call__
>     return client.invoke(args, kwargs)   File
> "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/client.py",
> line 602, in invoke
>     result = self.send(soapenv)   File "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/client.py",
> line 637, in send
>     reply = transport.send(request)   File
> "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/transport/https.py",
> line 64, in send
>     return  HttpTransport.send(self, request)   File
> "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/transport/http.py", line 77, in send
>     fp = self.u2open(u2request)   File "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/transport/http.py", line 118, in u2open
>     return url.open(u2request, timeout=tm)   File
> "/usr/lib/python2.6/urllib2.py", line
> 391, in open
>     response = self._open(req, data)   File "/usr/lib/python2.6/urllib2.py",
> line 409, in _open
>     '_open', req)   File "/usr/lib/python2.6/urllib2.py", lin开发者_Python百科e
> 369, in _call_chain
>     result = func(*args)   File "/usr/lib/python2.6/urllib2.py", line
> 1169, in https_open
>     return self.do_open(httplib.HTTPSConnection,
> req)   File
> "/usr/lib/python2.6/urllib2.py", line
> 1136, in do_open
>     raise URLError(err) urllib2.URLError: <urlopen error
> [Errno 111] Connection refused>

Please suggest if you know what is the problem here.


I did something like this

def client_with_token(token):
    header_ns = ('ns1', "http://4psa.com/HeaderData.xsd/3.5.0")
    access_token = Element('accessToken', ns=header_ns).setText(token)
    auth_header = Element('userCredentials', ns=header_ns)
    auth_header.append(access_token)
    client = Client(url)
    auth_header = get_auth_header()
    client.set_options(soapheaders=auth_header, *args, **kwargs)
    return client


The suds client has many that may be used to control the behavior of the library. Some are general options and others are transport options. Although, the options objects are exposed, the preferred and supported way to set/unset options is through:

  • The Client constructor
  • The Client.set_options()
  • The Transport constructor(s).

soapheaders – Provides for soap headers.

  • https://fedorahosted.org/suds/wiki/Documentation#OPTIONS
  • http://jortel.fedorapeople.org/suds/doc/suds.options.Options-class.html
  • http://jortel.fedorapeople.org/suds/doc/suds.client.Client-class.html#set_options
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜