I. java side
First of all, I use java comes with the support package for webservice to write the server-side and publishing program, the code is as follows.
Interface code for the webservice:
import ;
import ;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 14-3-5
* :: Time: 3:11 p.m.
*/
@WebService(targetNamespace = "/wsdl")
public interface CalculatorWs {
@WebMethod
public int sum(int add1, int add2);
@WebMethod
public int multiply(int mul1, int mul2);
}
Interface implementation code:
import ;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 14-3-5
* :: Time: 3:12 p.m.
*/
@WebService(
portName = "CalculatorPort",
serviceName = "CalculatorService",
targetNamespace = "/wsdl",
endpointInterface = "")
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 ;
import ;
import ;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 14-3-10
* :: Time: 3:10 p.m.
*/
public class CalclulatorPublisher {
public static void main(String[] args) {
("http://localhost:8080/test/calc", new Calculator());
//("http://10.3.18.44:8080/test/calc", new Calculator());
}
}[/code]
Run this code above to get your webservice running, next you can use Python to test your webservice code.
Once the above code is running, you can access it directly using your browser:
to verify that the startup was successful.
II. python end
Next is the python test code:
import suds
url = 'http://localhost:8080/test/calc?wsdl'
#url = 'http://10.3.18.44:8080/test/calc?wsdl'
client = (url)
service =
print client
sum_result = (10, 34)
print sum_result
print client.last_received()
multiply_result = (5, 5)
print multiply_result
print client.last_received()
Save the above code as a file and change the executable permissions again:
The output is as follows:
Service ( CalculatorService ) tns="/wsdl"
Prefixes (1)
ns0 = "/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>
III. Frequently asked questions
Note that it is possible to prompt when executing the code above:
File "", line 1, in <module>
import suds
ImportError: No module named suds
It says that dependent packages are missing, we can download and install suds packages manually.
tar zxvf suds-0.3.
cd suds-0.3.9
sudo python install
OK。