话不多少说,上代码:
python3 服务端:
from xmlrpc.server import SimpleXMLRPCServer
from socketserver import ThreadingMixIn
from xmlrpc.client import ServerProxy
import threading
class ThreadXMLRPCServer(ThreadingMixIn, SimpleXMLRPCServer):
pass
class RPCServer():
def __init__(self, ip='127.0.0.1', port='8000'):
self.ip = ip
self.port = int(port)
self.svr = None
def start(self, func_lst):
threading.start_new_thread(self.service, (func_lst, 0,))
def resume_service(self, v1, v2):
self.svr.serve_forever(poll_interval=0.001)
def service(self, func_lst, v1):
self.svr = ThreadXMLRPCServer((self.ip, self.port), allow_none=True)
for func in func_lst:
self.svr.register_function(func)
self.svr.serve_forever(poll_interval=0.001)
def activate(self):
threading.start_new_thread(self.resume_service, (0, 0,))
def shutdown(self):
try:
self.svr.shutdown()
except Exception as e:
print('rpc_server shutdown:', str(e))
class RPCClient():
def __init__(self, ip='127.0.0.1', port='8000'):
self.svr = ServerProxy('http://' + ip + ':' + port + '/', allow_none=True, use_datetime=True)
def get_svr(self):
return self.svr
def get_hello(year): # 有参数的传递,这里可以放置多个参数
print("获取参数 {}".format(year))
return "return" + str(year)
if __name__ == "__main__":
r = RPCServer('0.0.0.0', '8061')
print("启动rpc接口...")
r.service([get_hello], 0)
python3 客户端:
from xmlrpc.client import ServerProxy
import time
server = ServerProxy("http://localhost:8061")
time0 = time.time()
words = server.get_hello('da')
time1 = time.time()
print("result: {}, cost time {}".format(words, time1 - time0))
java 客户端:
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
class XmlRpcTest {
public static void main(String[] args) throws IOException {
System.out.println("before xml rpc111");
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://127.0.0.1:8061/RPC2"));
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
// 根据不同的python函数形式,构造参数
// 两个整形参数
//Object[] params = new Object[] {new Integer(1), new Integer(2)};
// 单个字符串参数
//Object[] params = new Object[] {new String("HELLO")};
// 无参数
Object[] params = new Object[] {new String("HELLO")};
// 放在要检测的代码段前,取开始前的时间戳
Long startTime = System.currentTimeMillis();
try {
// 返回的结果是字符串类型,强制转换res为String类型
String res = (String) client.execute("get_hello", params);
System.out.println(res);
} catch (XmlRpcException e11) {
e11.printStackTrace();
}
// 放在要检测的代码段后,取结束后的时间戳
Long endTime = System.currentTimeMillis();
// 计算并打印耗时
Long tempTime = (endTime - startTime);
System.out.println("花费时间:"+
(((tempTime/86400000)>0)?((tempTime/86400000)+"d"):"")+
((((tempTime/86400000)>0)||((tempTime%86400000/3600000)>0))?((tempTime%86400000/3600000)+"h"):(""))+
((((tempTime/3600000)>0)||((tempTime%3600000/60000)>0))?((tempTime%3600000/60000)+"m"):(""))+
((((tempTime/60000)>0)||((tempTime%60000/1000)>0))?((tempTime%60000/1000)+"s"):(""))+
((tempTime%1000)+"ms"));
// try {
//
// XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
// config.setServerURL(new URL("http://127.0.0.1:9091/XML_RPC_Server/service"));
//
// XmlRpcClient client = new XmlRpcClient();
// client.setConfig(config);
//
// Object[] params = new Object[]{new Integer(31), new Integer(9)};
// Integer result = (Integer) client.execute("MyCalculator.myAdd", params);
// System.out.println(result);
//
// } catch (XmlRpcException e) {
// e.printStackTrace();
// } catch (MalformedURLException e) {
// e.printStackTrace();
// }
}
}