[Twisted-Python] Help creating a client for a custom protocol
andrew-twisted at puzzling.org
Tue Jun 24 22:00:11 EDT 2003
On Tue, Jun 24, 2003 at 06:13:29PM -0500, Don Hiatt wrote:
> Sorry about the newbie question, but... :)
> I have a custom server running a byte-oriented protocol
> (sockets, written in C) and would like to use twisted to implement
> the client. I see from looking at the "simpleclient" how
> you can use an existing protocol (e.g. Echo) but I am not
> sure how to create a client using my own byte-oriented (e.g.
> "raw") protocol. If anyone could give me a little advice I
> would certainly appreciate it. :) Also, is ClientFactory appropriate
> for this sort of application?
Well, the 'data' parameter of an Protocol's dataReceived method is just a
string, i.e. bytes directly from the network. I've used Twisted in the past
for working with byte stream protocols -- it works very nicely.
A key part is the 'struct' module in Python's standard library, which can
convert bytes to numbers, and so forth. Take a look at the source to
twisted.protocols.basic.Int32Receiver, which receives strings prefixed by a
32-bit length. There's really nothing to it :)
Regarding ClientFactory -- yes, whenever you are using clients, you probably
want some sort of ClientFactory (quite possibly a ReconnectingClientFactory,
depending on the application). See
for an overview of using ClientFactory.
More information about the Twisted-Python