java的xmlrpc_xmlrpc

XML-RPC type

Python type

boolean

int, i1, i2, i4, i8 or biginteger

int in range from -2147483648 to 2147483647. Values get the tag.

double or float

float. Values get the tag.

string

array

list or tuple containing conformable elements. Arrays are returned as lists.

struct

dict. Keys must be strings, values may be any conformable type. Objects of user-defined classes can be passed in; only their __dict__ attribute is transmitted.

dateTime.iso8601

DateTime or datetime.datetime. Returned type depends on values of use_builtin_types and use_datetime flags.

base64

Binary, bytes or bytearray. Returned type depends on the value of the use_builtin_types flag.

nil

The None constant. Passing is allowed only if allow_none is true.

bigdecimal

decimal.Decimal. Returned type only.

server.py

#

import sys

import datetime

from xmlrpc.server import SimpleXMLRPCServer

from xmlrpc.server import SimpleXMLRPCRequestHandler

import xmlrpc.client

class RequestHandler(SimpleXMLRPCRequestHandler):

rpc_paths = ('/RPC2',)

server = SimpleXMLRPCServer(('localhost', 8000), requestHandler=RequestHandler, allow_none=True)

server.register_introspection_functions()

server.register_function(pow)

def adder_function(x, y):

return x + y

server.register_function(adder_function, 'add')

@server.register_function

def do_sth(x, y, z=None):

a = ''

if z:

a = x + y + z

else:

a = x + y

sys.stdout.write('okok\n')

class MyFuncs:

def mul(self, x, y):

return x * y

server.register_instance(MyFuncs())

@server.register_function

def today():

return xmlrpc.client.DateTime(datetime.datetime.today())

#return datetime.datetime.today()

server.serve_forever()

client.py

#

import datetime

import xmlrpc.client

s = xmlrpc.client.ServerProxy('http://localhost:8000')

#print(s.pow(2,3)) # Returns 2**3 = 8

print(s.add(2,3)) # Returns 5

#print(s.mul(5,2)) # Returns 5*2 = 10

try:

res = s.do_sth(9, 3, 8) # Returns 5

except xmlrpc.client.Fault as err:

print('A fault occurred')

print("Fault code: %d" % err.faultCode)

print("Fault string: %s" % err.faultString)

today = s.today()

converted = datetime.datetime.strptime(today.value, "%Y%m%dT%H:%M:%S")

print("Today: %s" % converted.strftime("%d.%m.%Y, %H:%M"))

#print(s.system.listMethods())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java XML-RPC 服务器是一种基于 XML-RPC 协议的服务器,可以通过网络接收来自客户端的请求,并根据请求进行相应的处理和响应。要实现一个 Java XML-RPC 服务器,可以使用 Apache XML-RPC 库或 Java 原生的 XML-RPC 库。 以下是一个简单的 Java XML-RPC 服务器的示例代码: ```java import java.net.*; import java.util.*; import org.apache.xmlrpc.*; public class MyServer { public static void main (String [] args) { try { WebServer server = new WebServer(8080); XmlRpcServer xmlRpcServer = server.getXmlRpcServer(); PropertyHandlerMapping phm = new PropertyHandlerMapping(); phm.addHandler("MyHandler", MyHandler.class); xmlRpcServer.setHandlerMapping(phm); server.start(); System.out.println("Server started on port 8080"); } catch (Exception e) { System.err.println("Server error: " + e.getMessage()); } } public static class MyHandler { public String echo(String msg) { return "Echo: " + msg; } public int add(int x, int y) { return x + y; } public Map<String, String> getInfo() { Map<String, String> info = new HashMap<>(); info.put("name", "MyServer"); info.put("version", "1.0"); return info; } } } ``` 在上面的代码中,我们创建了一个基于端口号 8080 的 WebServer 对象,并通过 PropertyHandlerMapping 将 MyHandler 类中的方法映射到 XML-RPC 服务器中。在 MyHandler 类中,我们定义了三个简单的方法:echo、add 和 getInfo,它们分别用于返回输入的字符串、两个整数的和以及一个包含服务器信息的 Map 对象。 要使用该服务器,可以创建一个 XML-RPC 客户端,并向服务器发送请求。例如,以下是一个使用 Apache XML-RPC 库的客户端的示例代码: ```java import java.net.*; import java.util.*; import org.apache.xmlrpc.*; public class MyClient { public static void main (String [] args) { try { XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); config.setServerURL(new URL("http://localhost:8080/RPC2")); XmlRpcClient client = new XmlRpcClient(); client.setConfig(config); String result1 = (String) client.execute("MyHandler.echo", new Object[]{"Hello, world!"}); int result2 = (int) client.execute("MyHandler.add", new Object[]{2, 3}); Map<String, String> result3 = (Map<String, String>) client.execute("MyHandler.getInfo", new Object[]{}); System.out.println(result1); System.out.println(result2); System.out.println(result3); } catch (Exception e) { System.err.println("Client error: " + e.getMessage()); } } } ``` 在上面的客户端代码中,我们创建了一个 XmlRpcClientConfigImpl 对象,并将服务器的 URL 设置为 http://localhost:8080/RPC2,然后创建了一个 XmlRpcClient 对象,并使用 execute 方法向服务器发送请求。在每个请求中,我们都指定了要调用的方法和传递给方法的参数。最后,我们使用结果对象中的数据来输出结果。 这只是一个简单的 Java XML-RPC 服务器和客户端的示例,实际上可以根据具体的需求进行更复杂的实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值