java 调用远程python_基于XML-RPC的远程调用(Python,Java)

基于XML-PRC的远程调用示例。

Ref:http://www.ibm.com/developerworks/cn/webservices/ws-pyth/part10/

Ref: http://xmlrpc.scripting.com/spec

・XML-RPC日历服务器

import calendar, SimpleXMLRPCServer

#The server object

class Calendar:

def getMonth(self, year, month):

return calendar.month(year, month)

def getYear(self, year):

return calendar.calendar(year)

calendar_object = Calendar()

server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 8888))

server.register_instance(calendar_object)

#Go into the main listener loop

print "Listening on port 8888"

server.serve_forever()

・请求日历服务器(Python)

import xmlrpclib

server = xmlrpclib.ServerProxy("http://localhost:8888")

month = server.getMonth(2012, 5)

print month

・请求日历服务器(Java)

importjava.io.BufferedReader;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.InputStreamReader;

importjava.net.HttpURLConnection;

importjava.net.URL;

publicclassWebClient {

publicstaticvoidmain(String[] args)throwsIOException {

WebClient wc = newWebClient();

wc.send();

}

privatevoidsend()throwsIOException {

HttpURLConnection conn = null;

String str = "<?xml version=\"1.0\"?>"+

""+

"getMonth"+

""+

""+

"2012"+

"5"+

""+

""+

"";

//URL url = new URL("http://10.167.12.51/RPC2:8888");

URL url = newURL("http://localhost:8888");

conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("POST");

conn.setRequestProperty("User-Agent","Frontier/5.1.2 (WinXP)");

conn.setRequestProperty("Content-Type","text/xml");

conn.setRequestProperty("Content-length", String.valueOf(str.length()));

conn.setDoOutput(true);

conn.setDoInput(true);

conn.setUseCaches(false);

//Request

conn.getOutputStream().write(str.getBytes());

conn.getOutputStream().flush();

conn.getOutputStream().close();

//Response

InputStream in = conn.getInputStream();

BufferedReader bufferedReader =newBufferedReader(newInputStreamReader(in));

String line = bufferedReader.readLine();

while(line !=null) {

System.out.println(line);

line = bufferedReader.readLine();

}

bufferedReader.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值