Wiki

Clone wiki

puck / Home

Puck

Stateful RPC over TCP/JSON for Python

Puck is a small Python library for doing RPC, but keeping a persistent connection to the server, and maintaining a remote stack. This means that Puck is agnostic to the content, as all objects will stay on the servers stack, and only simple proxy objects will be available to the client.

These proxy objects will appear to be instances of the remote object, and will even retain __doc__ and __repr__, but any method invocation will happen on the server. It's not transparent, it's translucent.

Puck builds on Twisted, a very scalable framework for networking in Python.

Terminology

First thing you are going to need is a Hub. A Hub is basically a server, which defines the operations available for the client to call. All methods that are exported must be prefixed remote_. Your Hub must subclass puck.hub.Hub.

Protocol

Documented more thoroughly in Protocol. In short, it's all JSON, so you could hook it up to most languages, like Perl or even Javascript.

Simple example

The Hub (examplehub.py):

from puck.hub import Hub, HubFactory

class ExampleHub(Hub):
   def remote_file(self, filename, mode='w'):
      return file(filename, mode)

class ExampleHubFactory(HubFactory):
   protocol = ExampleHub

The client (client.py):

from puck.client import PuckClient

c = PuckClient('localhost', 8080)
f = c.file('/tmp/testfile')

f.write('This is being written on the remote server.')
f.close()

f = c.file('/tmp/testfile', 'r')
print f.readline()
f.close

c.disconnect()

And finally the tac (examplehub.tac):

from twisted.application import service, internet
from examplehub import ExampleHubFactory

def getHub():
    return internet.TCPServer(8080, ExampleHubFactory())
    
application = service.Application("examplehub server")
service = getHub()
service.setServiceParent(application)

You can now run this with:

twistd -noy examplehub.tac

And you now have a working hub + client + twistd deployment server!

Updated