Anonymous user
Distributed programming: Difference between revisions
Add python example, see ghop task; http://code.google.com/p/google-highly-open-participation-psf/issues/detail?id=228
(Add python example, see ghop task; http://code.google.com/p/google-highly-open-participation-psf/issues/detail?id=228) |
|||
Line 105:
log "shoe";
find "foo"
=={{header|Python}}==
'''Version:''' 2.4 and 2.6, code will work on both
'''Protocol:''' XML-RPC
=== Server ===
<pre><nowiki>
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import SimpleXMLRPCServer
class MyHandlerInstance:
def echo(self, data):
'''Method for returning data got from client'''
return 'Server responded: %s' % data
def div(self, num1, num2):
'''Method for divide 2 numbers'''
return num1/num2
def foo_function():
'''A function (not an instance method)'''
return True
HOST = "localhost"
PORT = 8000
server = SimpleXMLRPCServer.SimpleXMLRPCServer((HOST, PORT))
# register built-in system.* functions.
server.register_introspection_functions()
# register our instance
server.register_instance(MyHandlerInstance())
# register our function as well
server.register_function(foo_function)
# serve forever
server.serve_forever()
</nowiki></pre>
=== Client ===
<pre><nowiki>
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xmlrpclib
HOST = "localhost"
PORT = 8000
rpc = xmlrpclib.ServerProxy("http://%s:%d" % (HOST, PORT))
# print what functions does server support
print 'Server supports these functions:',
print ' '.join(rpc.system.listMethods())
# echo something
rpc.echo("We sent this data to server")
# div numbers
print 'Server says: 8 / 4 is: %d' % rpc.div(8, 4)
# control if foo_function returns True
if rpc.foo_function():
print 'Server says: foo_function returned True'
</nowiki></pre>
|