国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python調(diào)用java的Webservice示例

2019-11-25 18:29:42
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一、java端
首先我使用的是java自帶的對(duì)webservice的支持包來(lái)編寫(xiě)的服務(wù)端和發(fā)布程序,代碼如下。
webservice的接口代碼:

復(fù)制代碼 代碼如下:
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);
}


接口實(shí)現(xiàn)代碼:
復(fù)制代碼 代碼如下:
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;
    }
}


發(fā)布Webservice代碼:[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]
運(yùn)行上面的這段代碼,讓你的webservice跑起來(lái),接下來(lái)就可以使用Python來(lái)測(cè)試你的webservice代碼了。
上面的代碼跑起來(lái)后,你可以直接使用瀏覽器訪問(wèn):

復(fù)制代碼 代碼如下:
http://localhost:8080/test/calc?wsdl

來(lái)驗(yàn)證是否啟動(dòng)成功。
二、python端
接下來(lái)是python的測(cè)試代碼:
復(fù)制代碼 代碼如下:
#!/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()

將上述代碼保存成webservice.py文件,再修改一下可執(zhí)行權(quán)限:

復(fù)制代碼 代碼如下:
chmod +x webservice.py

輸出結(jié)果如下:

復(fù)制代碼 代碼如下:
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>

三、常見(jiàn)問(wèn)題

注意,在執(zhí)行上面的代碼時(shí),有可能提示:

復(fù)制代碼 代碼如下:
Traceback (most recent call last):
  File "ws.py", line 1, in <module>
    import suds
ImportError: No module named suds

說(shuō)缺少依賴(lài)的包,我們可以手工下載安裝suds包。
復(fù)制代碼 代碼如下:
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。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 北京市| 鸡泽县| 隆子县| 许昌县| 巴南区| 清河县| 海阳市| 辽宁省| 上杭县| 梁平县| 洪泽县| 鲜城| 嘉善县| 祁阳县| 香港| 陵水| 海城市| 孙吴县| 泰来县| 邢台市| 富源县| 太康县| 清水河县| 巴林左旗| 拜泉县| 和林格尔县| 武邑县| 双鸭山市| 平安县| 嵩明县| 关岭| 西丰县| 蒙城县| 聊城市| 平顶山市| 阳谷县| 泾源县| 呼和浩特市| 通渭县| 京山县| 晴隆县|