XML-RPC type | Python type |
---|---|
boolean | bool |
int, i1, i2, i4, i8 or biginteger | int in range from -2147483648 to 2147483647. Values get the <int> tag. |
double or float | float. Values get the <double> tag. |
string | str |
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())