服务端代码
#coding=utf-8
__author__ = 'nate'
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
from datetime import datetime
class Test:
def __init__(self):
self.datetime = datetime
def test_in_class(self, a, b):
return a + b
def _test_private(self):
print 'test private funcation'
def test(a, b):
return a+b
if __name__ == '__main__':
port = 1234
try:
server = SimpleXMLRPCServer(('localhost', port), SimpleXMLRPCRequestHandler, allow_none=True)
server.register_function(test, 'test')
server.register_instance(Test(), allow_dotted_names=True)
print 'listening on port {port}'.format(port=port)
server.serve_forever()
except KeyboardInterrupt:
print 'exit'
客户端代码
#coding=utf-8
__author__ = 'nate'
import xmlrpclib
if __name__ == '__main__':
try:
port = 1234
server = xmlrpclib.ServerProxy('http://localhost:{port}'.format(port=port))
print server.test(1, 2)
print server.test_in_class(3, 4)
print server.datetime.now()
print server._test_private()
except xmlrpclib.Fault, fault:
print fault.faultString