Documentation for pulsar 0.7.2. For development docs, go here.
This example illustrates how to write a simple TCP Echo server and client pair. The example is simple because the client and server protocols are symmetrical and therefore the EchoProtocol will also be used as based class for EchoServerProtocol. The code for this example is located in the examples.echo.manage module.
There are two classes one needs to implement in order to have a flexible client for Echo servers, or for any TCP servers.
The first class implements the pulsar.ProtocolConsumer as it is described in the next session, while the second class implements the pulsar.Client which is a thread safe pool of connections to remote servers.
Pulsar provides additional classes for writing clients handling multiple requests. Here we subclass the pulsar.Client and implement the Echo.request method. Echo is the main client class, used in all interactions with the echo server:
>>> pool = Echo() >>> echo = pool.client(('127,0,0,1', 8080)) >>> response = echo(b'Hello!')
To run the server:
Open a new shell, in this directory, launch python and type:
>>> from manage import Echo >>> echo = Echo(force_sync=True).client(('localhost',8060))
>>> echo(b'Hello') b'Hello'
An echo pulsar.ProtocolConsumer for client and servers.
The only difference between client and server is the implementation of the response() method.
A separator for messages.
The buffer for long messages
Implements the pulsar.Protocol.data_received() method.
Override pulsar.Protocol.start_request() to write the message ended by the separator into the transport.
Utility for returning a function which interact with one server.
Build the client request send it to the server.