Download presentation
Presentation is loading. Please wait.
Published byJane Oliver Modified over 9 years ago
1
C. Varela, RPI1 Distributed Computing with Python Carlos Varela Rensselaer Polytechnic Institute
2
C. Varela, RPI2 Distributed Computing with Python Using standard TCP/IP (or UDP) sockets Using high-level libraries for remote method invocation –XML-RPC –Pyro (PYthon Remote Objects)
3
C. Varela, RPI3 Networking with Python Sockets Resembles C socket API Can use UDP (datagrams) or TCP (streamed connections) Does not require “non-standard” libraries Low-level, yet relatively easy to use and portable Enables more efficient communication and is programming language independent. New features require protocol extensions
4
C. Varela, RPI4 Sockets: An Echo Server from socket import * def main(): s = socket(AF_INET, SOCK_STREAM) s.bind((‘’, 50007)) # port number s.listen(1) conn, (remotehost, remoteport) = s.accept() print 'connected by', remotehost, remoteport while 1: data = conn.recv(1024) # buffer size if not data: break conn.send(data) conn.close() main()
5
C. Varela, RPI5 Sockets: An Echo Client from socket import * def main(): s = socket(AF_INET, SOCK_STREAM) s.connect((‘localhost’, 50007)) # port number s.send(‘Hello World’) data = s.recv(1024) # buffer size s.close() print data main()
6
C. Varela, RPI6 Remote Procedure Calls with Python XML-RPC library XML-RPC is programming-language/O.S. independent Uses XML as message format for parameters and return values, e.g.: Hello World Uses HTTP as the transport protocol. Enabling technology for building “web services”. Example requires downloading xmlrpcserver.py library: http://svn.python.org/view/python/trunk/Demo/xmlrpc/
7
C. Varela, RPI7 XML-RPC: An Echo Server import SocketServer; import xmlrpcserver; import xmlrpclib class EchoReqHandler(xmlrpcserver.RequestHandler): def call(self, method, params): print "Dispatching: ", method, params try: server_method = getattr(self, method) except: raise AttributeError, ”No method: %s" % method return server_method(params) def echo(self, value): return xmlrpclib.dumps(value) server = SocketServer.TCPServer(('', 8000), EchoReqHandler) server.serve_forever()
8
C. Varela, RPI8 XML-RPC: An Echo Client import xmlrpclib echosvr = xmlrpclib.Server("http://localhost:8000") print echosvr.echo("Hello World") print echosvr.echo(10)
9
C. Varela, RPI9 Distributed Computing with Python Remote Objects (PYRO) Resembles Java RMI: Server Daemon, Client Proxy To find remote objects: –By name, e.g., “PYRONAME://echo”, requires name server –By location, e.g., “PYROLOC://localhost:7766/echo” –By URI, e.g., “PYRO://127.0.0.1:7766/7f0000010512127841288b1892e90a71” Method arguments, return values, and exceptions marshalled (“pickled”) over the network Example code requires downloading and installing Pyro at: http://pyro.sourceforge.net/
10
C. Varela, RPI10 Pyro: An Echo Server import Pyro.core class Echo(Pyro.core.ObjBase): def __init__(self): Pyro.core.ObjBase.__init__(self) def echo(self, string): return string; Pyro.core.initServer() daemon=Pyro.core.Daemon() uri=daemon.connect(Echo(),"echo-object") print "The object's uri is:",uri daemon.requestLoop()
11
C. Varela, RPI11 Pyro: An Echo Client import Pyro.core echoProxy = Pyro.core.getProxyForURI( "PYROLOC://localhost:7766/echo-object") print echoProxy.echo("Hello World")
12
C. Varela, RPI12 Python Remote Objects (PYRO) Python programming language-dependent Event server for publish-subscribe communication Mobile code, but BEWARE: No security guarantees!
13
C. Varela, RPI13 Exercises 1.Compare the round trip time of the Echo application using: TCP/IP XML-RPC Pyro 2.Use the XML-RPC client in Python to connect to an existing web service (e.g., Google) http://www.xmlrpc.com/googleGateway 3.Modify the echo example in Pyro to use the name service. What are alternative ways to get to the name server? 4.Does Pyro support distributed garbage collection?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.