Detailed steps and instance code for using soaplib to build webservice

Source: Internet
Author: User
This article mainly introduces the detailed steps and instance code for using pythonsoaplib to build webservice. you can refer to using the python-based webservice project recently. Today, it has been a lot of time to configure the environment, record the configuration process and use it later:
First of all, you must have python on your system. you don't have to say that I use 2.7 + on your system.
Second, to use python for webservice development, you also need some libraries:
Lxml:
Run sudo easy_install lxml on the command line to install

Pytz:
Run sudo easy_install pytz to install

Soaplib:
The library required for webservice development. you can install it in composer python setup. py install.

The code is as follows:


Soaplib is an easy to use python library for publishing soap web services using WSDL 1.1 standard, and answering SOAP 1.1 requests. with a very small amount of code, soaplib allows you to write a useful web service and deploy it as a WSGI application.

Soaplib is a python soap framework that can be used to build webservice. soaplib. download here: https://github.com/volador/soaplib

After soaplib is installed, create a test. py and a webservice to return the sum of the two integers. The code is as follows:

The code is as follows:


#-*-Coding: cp936 -*-
Import soaplib
From soaplib. core. util. wsgi_wrapper import run_twisted # publish a service
From soaplib. core. server import wsgi
From soaplib. core. service import DefinitionBase # All service classes must inherit this class
From soaplib. core. service import soap # declare annotation
From soaplib. core. model. clazz import Array # declare the type to be used
From soaplib. core. model. clazz import ClassModel # if the service returns a class, the returned class must be a subclass of the class.
From soaplib. core. model. primitive import Integer, String
Class C_ProbeCdrModel (ClassModel ):
_ Namespace _ = "C_ProbeCdrModel"
Name = String
Id = Integer
Class AdditionService (DefinitionBase): # this is a web service
@ Soap (Integer, Integer, _ returns = String)
Def addition (self, a, B ):
Return str (a) + '+ str (B) +' = '+ str (a + B)
@ Soap (_ returns = Array (String ))
Def GetCdrArray (self ):
Rochelle result = ["1", "2", "3"]
Return L_Result
@ Soap (_ returns = C_ProbeCdrModel)
Def GetCdr (self): # The returned class must be a subclass of ClassModel, which has been defined above
L_Model = C_ProbeCdrModel ()
Rochelle model.name = Rochelle model.name
L_Model.Id = L_Model.Id
Return L_Model


If _ name __= = '_ main _': # publish a service
Try:
Print 'service enabled'
From wsgiref. simple_server import make_server
Soap_application = soaplib. core. Application ([AdditionService], 'tns ')
Wsgi_application = wsgi. Application (soap_application)
Server = make_server ('localhost', 7789, wsgi_application)
Server. serve_forever ()

Failed T ImportError:
Print 'error'

Access http: // 127.0.0.1: 7789/SOAP/? in a browser /? In wsdl, a large version of xml is displayed instead of an access error, indicating that the service is successfully added.
You can test

The code is as follows:


>>> From suds. client import Client
>>> Test = Client ('http: // localhost: 7789/SOAP /? Wsdl ')
>>> Print test. service. addition (1, 2)
1 + 2 = 3

Note: You can also decompress the command and switch to the decompressed directory in cmd. execute: python setup. py install to install it successfully.
The first time a connection is established, it is slow and unbearable. >>> test = Client ('http: // localhost: 7789/SOAP /? Wsdl '). I don't know what is going on.

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.