Thank you for your answer
Depends on what protocol you want to use. XML-RPC is simplest: make an instance of SimpleXMLRPCServer.SimpleXMLRPCServer (using the module in the standard Python library), record callables it needs to make available with its register_function method, then call its serve_forever
method -- you're done.
Other protocols, as usual, are more complicated -- you'll need to download and install some suitable third-party library and use it. For example, for SOAP, you could use soaplib (there are also other third-party libraries for writing SOAP servers in Python).
Actually, the simplest I can think of is to use Twisted 'Perspective Broker' which was designed specifically for that. If you only need Python methods available remotely, no need to sweat it with protocol stuff; from the Twisted Documentation:
Suppose you find yourself in control of both ends of the wire: you have two programs that need to talk to each other, and you get to use any protocol you want. If you can think of your problem in terms of objects that need to make method calls on each other, then chances are good that you can use twisted's Perspective Broker protocol rather than trying to shoehorn your needs into something like HTTP, or implementing yet another RPC mechanism.
I will now include a full client and server (for original author, see this):
Server:
from twisted.spread import pb
from twisted.internet import reactor
class Echoer(pb.Root):
def remote_echo(self, st):
print 'echoing:', st
return st
if __name__ == '__main__':
serverfactory = pb.PBServerFactory(Echoer())
reactor.listenTCP(8789, serverfactory)
reactor.run()
Client
from twisted.spread import pb
from twisted.internet import reactor
class EchoClient(object):
def connect(self):
clientfactory = pb.PBClientFactory()
reactor.connectTCP("localhost", 8789, clientfactory)
d = clientfactory.getRootObject()
d.addCallback(self.send_msg)
def send_msg(self, result):
d = result.callRemote("echo", "hello network")
d.addCallback(self.get_msg)
def get_msg(self, result):
print "server echoed: ", result
if __name__ == '__main__':
EchoClient().connect()
reactor.run()
That's it. The Client calls the remote echo
method (called remote_echo
) from the Server, which could be living in another side of the world. If you need to add your 20,000 other methods, the approach grows very smoothly.
In conclusion, if you want to simply call methods (and not deal with XML or protocol stuff) go, and use Twisted's Perspective Broker.