Python calls Java's webservice example _python

Source: Internet
Author: User
Tags wsdl

One, Java end
First I use the Java WebService Support package to write the server and the release program, the code is as follows.
WebService Interface Code:

Copy Code code as follows:
Package com.xxx.test.ws;

Import Javax.jws.WebMethod;
Import Javax.jws.WebService;

/**
* Created with IntelliJ idea.
* User:administrator
* Date:14-3-5
* Time: 3:11
*/
@WebService (targetnamespace = "http://xxx.com/wsdl")
Public interface Calculatorws {
@WebMethod
public int sum (int add1, int add2);

@WebMethod
public int multiply (int mul1, int mul2);
}


Interface Implementation code:
Copy Code code as follows:
Package com.xxx.test.ws;
Import Javax.jws.WebService;
/**
* Created with IntelliJ idea.
* User:administrator
* Date:14-3-5
* Time: 3:12
*/
@WebService (
PortName = "Calculatorport",
ServiceName = "CalculatorService",
targetnamespace = "HTTP://XXX.COM/WSDL",
Endpointinterface = "Com.xxx.test.ws.CalculatorWs")
public class Calculator implements CALCULATORWS {
public int sum (int add1, int add2) {
return add1 + add2;
}

public int multiply (int mul1, int mul2) {
return MUL1 * MUL2;
}
}


Publish WebService code: [Code
Package com.xxx.test.endpoint;
Import Com.xxx.test.ws.Calculator;
Import Javax.xml.ws.Endpoint;

/**
* Created with IntelliJ idea.
* User:administrator
* DATE:14-3-10
* Time: 3:10
*/
public class Calclulatorpublisher {
public static void Main (string[] args) {
Endpoint.publish ("Http://localhost:8080/test/calc", New Calculator ());
Endpoint.publish ("Http://10.3.18.44:8080/test/calc", New Calculator ());
}
}[/code]
Run the code above and let your webservice run, and then you can use Python to test your webservice code.
After the above code runs, you can access it directly using the browser:

Copy Code code as follows:
http://localhost:8080/test/calc?wsdl

To verify that the startup is successful.
Two, Python end
Next comes the Python test code:
Copy Code code as follows:
#!/usr/bin/python
Import Suds
url = ' http://localhost:8080/test/calc?wsdl '
#url = ' http://10.3.18.44:8080/test/calc?wsdl '
Client = suds.client.Client (URL)
Service = Client.service

Print Client

Sum_result = Service.sum (10, 34)
Print Sum_result
Print client.last_received ()

Multiply_result = service.multiply (5, 5)
Print Multiply_result
Print client.last_received ()

Save the above code as a webservice.py file, and then modify the executable permissions:

Copy Code code as follows:
chmod +x webservice.py

The output results are as follows:

Copy Code code as follows:
Suds (https://fedorahosted.org/suds/) version:0.3.9 (Beta) build:r658-20100210

Service (CalculatorService) tns= "http://xxx.com/wsdl"
   prefixes (1)
       NS0 = "http://xxx.com/wsdl"
   Ports (1):
      (Calculatorport)
         Methods (2):
             Multiply (xs:int arg0, Xs:int arg1,)
             sum (xs:int arg0, Xs:int arg1,)
         Types (4):
            Multiply
             Multiplyresponse
             sum
            sumresponse


44
<?xml version= "1.0" encoding= "UTF-8"?>
<S:Envelope>
<S:Body>
<ns2:sumResponse>
<return>44</return>
</ns2:sumResponse>
</S:Body>
</S:Envelope>
25
<?xml version= "1.0" encoding= "UTF-8"?>
<S:Envelope>
<S:Body>
<ns2:multiplyResponse>
<return>25</return>
</ns2:multiplyResponse>
</S:Body>
</S:Envelope>

Third, frequently asked questions

Note that when you execute the above code, you may be prompted:

Copy Code code as follows:
Traceback (most recent call last):
File "ws.py", line 1, in <module>
Import Suds
Importerror:no module named Suds

We can download and install the Suds package manually without relying on the package.
Copy Code code as follows:
wget http://downloads.sourceforge.net/project/python-suds/suds/0.3.9/suds-0.3.9.tar.gz?r=http%3A%2F% 2fsourceforge.net%2fprojects%2fpython-suds%2ffiles%2f&ts=1394436413&use_mirror=nchc
Tar zxvf suds-0.3.9.tar.gz
CD suds-0.3.9
sudo python setup.py install

Ok.

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.