0

I am trying to connect to a web service using Python/SUDS.

I have the following code in a single file and I am able to connect successfully and I receive a response.

class Suds_Connect:
    def __init__(self, url, q_user, q_passwd):

        logging.basicConfig(level=logging.INFO)
        logging.getLogger('suds.client').setLevel(logging.DEBUG)
        try:
            # fix broken wsdl
            # add <s:import namespace="http://www.w3.org/2001/XMLSchema"/> to the wsdl
            imp = Import('http://www.w3.org/2001/XMLSchema',
            location='http://www.w3.org/2001/XMLSchema.xsd')

            wsdl_url = url
            self.client = Client(wsdl_url, doctor=ImportDoctor(imp))
            t = HttpAuthenticated()


            security = Security()
            token = UsernameToken(q_user,q_passwd)
            security.tokens.append(token)
            self.client.set_options(wsse=security)



        except Exception, e: 
            print "Unexpected error:", sys.exc_info()[0]
            print str(e)
            sys.exit()


def CallWebMethod():

        try:
        print ' SUDS Client'
            print self.client
            Person= self.client.factory.create('ns0:Person')


            Person.name= 'bob'
            Person.age= '34'
            Person.address= '44, river lane'
            print self.client.service.AddPerson(Person)

        except WebFault, f:
            print str(f.fault)
        except Exception, e: 
            print str(e)

if __name__ == '__main__':
    errors = 0
    sudsClient = Suds_Connect('url','user','password')
    sudsClient.CallWebMethod()
    print '\nFinished:'

I want to use this code in a Python client app that will be called from a button click event. I have tried to implement this and I am able to print out the client but when I make the web service call (print self.client.service.AddPerson(Person)) I get the following error.

unsupported operand type(s) for +: 'NoneType' and 'str'

How do I go about fixing this error?

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
Nollaig
  • 1,097
  • 2
  • 12
  • 26

2 Answers2

0

Looks like webservice call returns None. Additional information is required to identify what went wrong.

First of all - enable suds logging by adding before calling Suds_Connect. This should provide you information about what's going on in suds under the hood.

import logging
logging.basicConfig(level=logging.DEBUG) 

Another thing to try for getting more debug information - please try the following instead of print self.client.service.AddPerson(Person)

result = self.client.service.AddPerson(Person)
print str(result)

Also it would be helpful to get full stack trace for exception - which line it happens at and what is the call stack. Please try to comment out exception handling for except Exception, e: case and post here the exception you'll get.

vvladymyrov
  • 5,715
  • 2
  • 32
  • 50
0

Ok,

It seems the issue I was having relates to the Soap Header generation. In SUDS there is a file wsse.py that contains a function xml().

def xml(self):
        """
        Get xml representation of the object.
        @return: The root node.
        @rtype: L{Element}
        """
    root = Element('UsernameToken', ns=wssens)
    u = Element('Username', ns=wssens)
    u.setText(self.username)
    root.append(u)
    p = Element('Password', ns=wssens)
    p.set('Type', "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest")

    import sha
    import base64
    p.setText(base64.encodestring(sha.new(self.nonce + str(UTC(self.created)) + self.password).digest()).replace("\n", ''))

    root.append(p)
    if self.nonce is not None:
        n = Element('Nonce', ns=wssens)
        n.setText(base64.encodestring(self.nonce).replace("\n",''))
        root.append(n)
    if self.created is not None:
        n = Element('Created', ns=wsuns)
        n.setText(str(UTC(self.created)))
        root.append(n)
    return root

I was getting an error @ the following line:

p.setText(base64.encodestring(sha.new(self.nonce + str(UTC(self.created)) + self.password).digest()).replace("\n", ''))

So I commented out this line and added the following line:

p.setText(self.password)

Note that I am making a call over https

regards

Noel

Nollaig
  • 1,097
  • 2
  • 12
  • 26