server:
#coding:utf-8
from SimpleXMLRPCServer import SimpleXMLRPCServer,SimpleXMLRPCRequestHandler
from SocketServer import ThreadingMixIn
class Math:
def pow(self,x,y):
return x**y
def hex(self,x):
return "%x"% x
class ForkingServer(ThreadingMixIn,SimpleXMLRPCServer):
pass
serveraddr=('',8765)
srvr=ForkingServer(serveraddr,SimpleXMLRPCRequestHandler)
srvr.register_instance(Math())
srvr.register_introspection_functions()
srvr.serve_forever()
client:
#coding:utf-8
import xmlrpclib,code
url="http://localhost:8765/"
s=xmlrpclib.ServerProxy(url)
interp=code.InteractiveConsole({'s':s})
interp.interact("you can now use the project s to interact with the server")
C:\Python27\python.exe D:/Python/demo/TCP_UDP/testclient.py
>>> you can now use the project s to interact with the server
>>> s.pow(2,8)
256
>>> s.hex(255)
'ff'
>>> s.system.listMethods()
['hex', 'pow', 'system.listMethods', 'system.methodHelp', 'system.methodSignature']
>>>