Python Web programming [2]-WebService technology [0]-using Python to invoke the WebService interface

Source: Internet
Author: User
Tags extend soap wsdl

WebService Technology /WebService Technology

1 about the webservice/constants

WebService is a remote invocation technique across programming languages and cross-operating system platforms.

WebService is comprised of the following three technologies,xml+xsd,SOAP and WSDL

Xml+xsd:

WebService transmits data using the HTTP protocol, encapsulating the data in XML format (that is, what method of invoking the remote service object in XML, what parameters are passed, and what the service object returns). XML is the format for representing data in the WebService platform. In addition to being easy to establish and easy to analyze, the main advantage of XML is that it is platform-independent and vendor-independent. Independence is more important than technical superiority: Software vendors do not choose a technology invented by competitors.

XML solves the problem of data representation , but it does not define a set of standard data types, much less how to extend this set of data types. For example, what does shaping number mean? 16-bit, 32-bit, 64-bit? These details are important for interoperability. XML Schema (XSD) is a set of standards specifically designed to address this problem. It defines a standard set of data types and gives a language to extend this set of data types. The WebService platform uses XSD as its data type system. When you construct a Web service in a language such as vb.net or C #, all data types that you use must be converted to an XSD type in order to conform to the WebService standard. The tool you used may have automatically helped you with the conversion, but you will probably have to modify the conversion process to suit your needs.

Soap:

WebService sends requests and receives results through the HTTP protocol, both the requested content and the resulting content are encapsulated in XML format , and some specific HTTP message headers are added to describe the content format of the HTTP message , these specific HTTP message headers and XML content formats are the SOAP protocol . SOAP provides a standard RPC method to invoke a Web Service.

SOAP protocol = HTTP protocol + XML data format

The SOAP protocol defines the format of the SOAP message, the SOAP protocol is based on the HTTP protocol, and SOAP is based on XML and XSD, and XML is the data encoding method of soap. A metaphor: HTTP is the normal highway, XML is the middle of the green barrier and both sides of the fence, soap is the ordinary highway through the belt and the fence has been modified highway.

Wsdl:

Like we go to the store to buy things, first of all, we have to know what is in the store to buy, and then to buy, the way the merchant is to put up advertising posters. WebService also, the WebService client to invoke a WebService service, first of all have to know the address of the service where, and what methods in this service can be called, so the WebService server side first through a WSDL file to explain what services in your home can be called externally, what the service is (what methods are available in the service, what parameters are accepted by the method, what the return value is), what URL address the network address of the service is used to represent, and how the service is invoked.

WSDL (web Services Description Language) is an XML-based language that describes a Web Service and its functions, parameters, and return values . It is a standard format that can be understood by both the WebService client and server side. Because it is XML-based, WSDL is both machine readable and human readable, which is a great benefit. Some of the latest development tools can generate WSDL documents based on your Web service, import WSDL documents, and generate proxy class code that calls the appropriate webservice.

The WSDL file is saved on the Web server and can be accessed through a URL address . Before the client wants to invoke a WebService service, it needs to know the address of the WSDL file for the service. The WebService service provider can expose its WSDL file address in two ways: 1. Register to a UDDI server to be searched; 2. Tell the caller directly to the client.

2 Suds Module /Suds Module

The Suds module can be used in Python to implement calls to WebService.

Environment Installation :

Pip Install Suds-jurko  

How to use :

(1) Establish initial data, username/password/url (URL is often WSDL page), etc.

(2) Instantiation of the Client,print after the instantiation of the client can see the WSDL functions and parameters, etc.

(3) using Client.servic to define a method call to WSDL

Sample Code :

The following code is a simple invocation example that allows you to see all the callable function interfaces by using the instance of print.

 fromSuds.clientImportClientclassbarapixserviceexception (Exception):PassclassBarapixwronguser (Exception):Passclassbarapixidnotfoundexception (Exception):PassclassService ():def __init__(Self, URL, userid, password, barapix_id, timeout=0): Self.client=None Self.userid=userid Self.password=Password self.barapix_id=barapix_idTry:            ifTimeout = =0:self.client= Client (url=URL)Else: Self.client= Client (Url=url, timeout=Timeout)except:            Raisebarapixserviceexception#Show the function of service        #print (self.client)Self.login ()defLogin (self):Try: Response=Self.client.service.WS_XStartup (Self.userid, Self.password)#Print (response)            ifresponse['Ws_xstartupresult'] ==0:RaiseBarapixwronguserPrint('= = = Login in Success')        except:            RaisebarapixserviceexceptiondefGet_id_info (self):Try: Response=Self.client.service.WS_XGetIdInfoBaso (Self.userid, self.barapix_id)ifresponse['Ws_xgetidinfobasoresult'] ==0:Raisebarapixidnotfoundexceptionexcept:            Raisebarapixserviceexception Return_response= {}         forRinchResponse:ifIsinstance (r[1], str): Return_response[str (r[0])= STR (r[1].strip ())Else:                #Handle None ValueReturn_response[str (r[0]) = r[1]        Print(Return_response)returnReturn_responsedefGet_test_status (self):Try: Response=self.client.service.WS_XGetTestStatus (Self.userid, self.barapix_id)ifresponse['Ws_xgetteststatusresult'] ==0:Raisebarapixidnotfoundexceptionexcept:            Raisebarapixserviceexception Return_response=ResponsereturnReturn_responseinfo= {'Service_1': {'userid':'User1',                      'Password':'Password',                      'barapix_id': ['dxxxxx'],                      'URL':'http://xxxxx/BarApix/BarApixService/?wsdl'                      },        'service_2': {'userid':'User2',                      'Password':'Password',                      'barapix_id': ['cxxxxx'],                      'URL':'http://xxxxxxx/BarApix/BarApixService/?wsdl'},}user= info['Service_1']service= Service (user['URL'], user['userid'], user['Password'], user['barapix_id'][0])#More Function Please check ' barApixFunc.txt 'Service.get_id_info () service.get_test_status ( )

Reference links

http://blog.csdn.net/wooshn/article/details/8069087/

Http://www.cnblogs.com/walkerwang/archive/2011/07/27/2118401.html

Http://www.cnblogs.com/nottobe/p/5718823.html

Http://www.cnblogs.com/Tommy-Yu/p/5567091.html

Python Web programming [2]-WebService technology [0]-using Python to invoke the WebService interface

Related Article

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.