Python tutorial for Strap

Pierre-Antoine Champin

This very short tutorial demonstrates how to use the Python binding of the Strap protocol.

Basic concepts

Strap has been designed to enable independant software component to share information represented as RDF graphs without having to exchange a complete serialized representation of that graph. It is a client-server protocol where the server acts as a graph provider, and the client acts as a graph user. The protocol provides 4 operations:

The first three operations are the basic operations on RDF graphs provided by most RDF APIs, including rdflib, so Strap can be embeded seamlessly in those APIs, as the examples below will demonstrate. Note that to run those examples, you will need to have rdflib installed.

The communication between the server and the client requires a bi-directionnal communication stream. This will most often be a TCP socket, but could also be a pair of pipes for components on the same system.

A quick start

The Strap source package provides two examples : simple_file_server and simple_persistent_server. The first one provides multiple read-only graphs, while the second one provides a single mutable graph. For example, launch :

python examples/simple_persistent_server 1234 

This creates a Strap server, listening on port 1234, accessible by Strap clients through the URL strap://localhost:1234/ . The only graph provided by this server is the root of the server, but other server (e.g. simple_file_server) can access different paths, like e.g. strap://localhost:1234/utest/tiny.rdf .

You can now use this graph through the Strap protocol with the tools provided in the package. For example :

python strapdump.py strap://localhost:1234/

will print an XML serialization of that graph.

python strapfeed.py strap://localhost:1234/ utest/tiny.rdf

will add all the triples from the given file to the graph (check it by calling strapdump) again).

Have a look at the python program for or : they are plainly using the rdflib API to handle a graph. The only difference is when creating the graph, which is done like

from rdflib                   import Graph
from strap.rdflib_impl.client import StrapBackend

g = Graph (StrapBackend (a_strap_url))

After that, g is used as any other rdflib graph. The difference is that the graph is provided by an independant program, possibly developed in another language and running on a different machine.