当前位置: 首页 > 工具软件 > XMLRPC++ > 使用案例 >

xmlrpc

巴照
2023-12-01
XML-RPC typePython type
booleanbool
int, i1, i2, i4, i8 or bigintegerint in range from -2147483648 to 2147483647. Values get the <int> tag.
double or floatfloat. Values get the <double> tag.
stringstr
arraylist or tuple containing conformable elements. Arrays are returned as lists.
structdict. 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.iso8601DateTime or datetime.datetime. Returned type depends on values of use_builtin_types and use_datetime flags.
base64Binary, bytes or bytearray. Returned type depends on the value of the use_builtin_types flag.
nilThe None constant. Passing is allowed only if allow_none is true.
bigdecimaldecimal.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())

转载于:https://my.oschina.net/redhands/blog/3062893

 类似资料:

相关阅读

相关文章

相关问答