WebService example of Python calling Java

Source: Internet
Author: User
One, the Java side
First I used Java's own support package for WebService to write the server side and the publisher, the code is as follows.
Interface code for WebService:
Copy CodeThe code is 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 CodeThe code is 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 above code and let your webservice run, and then you can use Python to test your webservice code.
After running the above code, you can access it directly using the browser:
Copy the Code code as follows:

http://localhost:8080/test/calc?wsdl


To verify that the startup is successful.
Two, Python end
Next is the Python test code:
Copy CodeThe code is 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 the Code code as follows:

chmod +x webservice.py

The output results are as follows:
Copy the 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




44



25




25


Third, frequently asked questions

Note that it is possible to prompt when executing the above code:
Copy the Code code as follows:

Traceback (most recent):
File "ws.py", line 1, in
Import Suds
Importerror:no module named Suds


We can manually download and install the SUDS package if we say we are missing a dependent package.
Copy the 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.

  • 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.