Python publishes and invokes soap-based WebService

Source: Internet
Author: User
Tags soap wsdl

Today's service-oriented (SOA) architecture design has become mainstream, it is a very common practice to package public services into WebService for all parties, and the most widely used is based on the SOAP protocol and WSDL webservice. This article explains how to publish and invoke a SOAP-based webservice in the Python environment, based on Soaplib (publish) and Suds (Invoke).

Os:ubuntu 14.04 python:2.7.6

Service side:

1. Installation:

The toolkit to be used by the server is Soaplib, unfortunately it is now also discontinued, but fortunately it can be used in Https://github.com/soaplib/soaplib, whose official documents are in http://soaplib.github.io/soaplib/2_0/

First download the zip file directly, and then run the Python setup.py install directly after unpacking.

2. Write a WebService service:

Next up is a chestnut, and here's a webservice that takes a string type parameter and returns a string type.

1 ImportSoaplib2  fromSoaplib.core.model.primitiveImportString3  fromSoaplib.core.serverImportWsgi4  fromSoaplib.core.serviceImportDefinitionbase#all service classes inherit the Definitionbase base class5  fromSoaplib.core.serviceImportSoap#characteristics of the SOAP identity method6 7  fromModelImportFeedforward8 9 Ten classwebserver (definitionbase): One@soap (String, _returns=String) A     defGetmodel (self, input): -         return' Hello World ' -  the if __name__=='__main__': -     Try: -          fromWsgiref.simple_serverImportMake_server -Soap_application = Soaplib.core.Application ([webserver],'TNS','WebService') +Wsgi_application =Wsgi. Application (soap_application) -  +         Print "Listening to http://0.0.0.0:7789" A         Print "WSDL is at:http://172.11.0.11:7789/?wsdl" at  -Server = Make_server ('172.11.0.11', 7789, Wsgi_application) - Server.serve_forever () -  -     exceptImporterror: -         Print "error:example Server code requires Python >= 2.5"

Note the SOAP annotations on line 11th

@soap (String, _returns=string)

Indicates that the annotated method Getmodel needs to accept a string-type argument, and _returns=string indicates that the parameter returned by the method is also of type string. Of course, you can also set more complex parameter types or custom types, such as:

1 @soap (user, _returns=String)2def  GetUser (self, User):3      name = user. Name4      return name

Indicates that the import parameter is a custom type of user, the argument is a string, but the custom type must inherit the Classmodel class:

class User (Classmodel):     __namespace__ " User "     Name=string

If the type returned is a collection that requires an array type of soaplib, for example:

@soap (_returns=Array (String))     def  Getcdrarray (self):        l_result=[" 1 ","2","3"# Returns the format of the collection data         return L_result

3. Release WebService

In the example above, starting with the 15th line if __name__ = = ' __main__ ' is the WebService release process. First, you create a Application object:

Soap_application = Soaplib.core.Application ([webserver], ' TNS ', ' WebService ')

According to the documentation, where the first parameter represents an iterable of ServiceBase subclasses that define the exposed services. is to load the class of all the services you want to publish into a list as the first parameter of the method

  The second parameter indicates that the targetnamespace attribute of the exposed service is the WebService namespace, and the default is TNS

The third parameter indicates that the name attribute of the exposed service defaults to None

Next, convert the soap_application to a wsgi_application:wsgi_application = Wsgi. Application (soap_application)

Finally, create a new server object, set the IP and port, and then start:

Server = Make_server (' 172.11.0.11 ', 7789, wsgi_application)
Server.serve_forever ()
This will release a webservice, and then enter HTTP://172.11.0.11:7789/?WSDL in the browser to see the corresponding WSDL file.

Client:

Client we use SUDS call WebService

1. Installation

First or the installation, the official homepage https://fedorahosted.org/suds/, download the version you want, and then unzip, Python setup.py install, and the installation soaplib exactly the same. Document address in Https://fedorahosted.org/suds/wiki/Documentation

2. Use

Calling WebService with Suds is straightforward.

     

ImportSudsImportLogginglogging.basicconfig ( level=logging.info)classwebservice_client ():def __init__(self, url): Self.client=suds.client.Client (URL)#get model from Smile    defGet_model (self, input):Try: Model=Self.client.service.GetModel (Input)returnModelexceptException,e:logging.info ('failed to get model:'+str (e))returnNoneif __name__=='__main__': Test= Webservice_client ('http://172.11.30.211:7789/?wsdl')    PrintTest.get_model ('347349')

Run this script and you can get the return value Hello World.

The above is a method for Python to publish and invoke a WSDL type WebService based on the SOAP protocol

    

  

Python publishes and invokes soap-based WebService

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.