Using SOAPpy to Access SOAP Web Services Through a WSDL File


from SOAPpy import WSDL wServer = WSDL.Proxy( \         'http://api.google.com/GoogleSearch.wsdl') print wServer.methods.keys() methodData = wServer.methods['doGoogleSearch'] for p in methodData.inparams:     print "  %s %s" % (p.name.ljust(12), p.type[1]) hits = wServer.doGoogleSearch(key, searchStr, 0, \         10, False, "", False, "", "utf-8", "utf-8") print len(hits.resultElements), "Hits . . ." for hit in hits.resultElements:     print "\nURL:", hit.URL     print "Title:", hit.title     print "Desc:", hit.snippet

The dynamics of the Python language make it a perfect fit for SOAP web services. The SOAPpy module, available at http://pywebsvcs.sourceforge.net/, includes functions that enable you to create Python scripts that allow you to access SOAP web services.

This phrase is designed to familiarize you with using the SOAPpy module to access SOAP web services through a Web Service Definition Language (WSDL) file. A WSDL file is an XML file that describes the URL, namespace, type of web service, functions, arguments, argument data types, and function return values of the SOAP web service. In this case, the sample code accesses the Google search SOAP web service through the GoogleSearch.wsdl file.

The first step is to create an instance of the WSDL proxy server using the WSDL.Proxy(wsdlfile) function of the SOAPpy module. The WSDL.Proxy function accepts a WSDL filename as its only argument and returns a WSDL proxy server object.

After you have created the WSDL proxy server object, you can view the available methods using the methods attribute of the WSDL proxy server object, as shown in the sample code wServer.methods.keys(). The methods attribute is a dictionary containing the available methods of the web service.

To view the arguments associated with a specific method, look up the method in the dictionary to get a method data object, as shown in the sample code Server.methods['doGoogleSearch']. Once you have the method data object, the arguments can be accessed using the inparams attribute, which is a list of parameter objects. The name and type of the parameter are available using the name and type attributes of the parameter object, as shown in the sample code p.name.ljust(12), p.type[1]).

The methods on the SOAP server can be called as methods of the WSDL proxy server object using the "." syntax as shown in the example soap_wsdl.py.

Note

This phrase focuses on using Google's SOAP web service; however, there are numerous services out there that can be accessed in much the same way. A good place to start is to look at the services provided at http://www.xmethods.net/.


Note

In the sample code, key is set to INSERT_YOUR_KEY_HERE. You will need to go to http://api.google.com and create an account to get your own key. Once you have your own key, insert it into the sample code.


from SOAPpy import WSDL searchStr = 'python' key = 'INSERT_YOUR_KEY_HERE' #Create WSDL server object wServer = WSDL.Proxy( \         'http://api.google.com/GoogleSearch.wsdl') #Display methods print "\nAvailable Methods\n======================" print wServer.methods.keys() #Display method arguments print "\ndoGoogleSearch Args\n====================" methodData = wServer.methods['doGoogleSearch'] for p in methodData.inparams:     print "  %s %s" % (p.name.ljust(12), p.type[1]) #Call method hits = wServer.doGoogleSearch(key, searchStr, 0, \         10, False, "", False, "", "utf-8", "utf-8") #Print results print "\nResults\n===============================" print len(hits.resultElements), "Hits . . ." for hit in hits.resultElements:     print "\nURL:", hit.URL     print "Title:", hit.title     print "Desc:", hit.snippet


soap_wsdl.py

Available Methods ====================== [u'doGoogleSearch', u'doGetCachedPage',  u'doSpellingSuggestion'] doGoogleSearch Args ====================   key          string   q            string   start        int   maxResults   int   filter       boolean   restrict     string   safeSearch   boolean   lr           string   ie           string   oe           string Results =============================== 10 Hits . . . URL: http://www.python.org/ Title: <b>Python</b> Language Website Desc: Home page for <b>Python</b>, an interpreted, interactive, object-oriented, extensible<br> programming language. It provides an extraordinary combination of clarity and <b>...</b> URL: http://www.python.org/download/ Title: Download <b>Python</b> Software Desc: The original implementation of <b>Python</b>, written in C. URL: http://www.python.org/doc/ Title: <b>Python</b> Documentation Index Desc: Official tutorial and references, including library/module usage, Macintosh<br>  libraries, language syntax, extending/embedding, and the <b>Python</b>/C API. . . .


Output of soap_wsdl.py



Python Phrasebook(c) Essential Code and Commands
Python Phrasebook
ISBN: 0672329107
EAN: 2147483647
Year: N/A
Pages: 138
Authors: Brad Dayley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net