[Twisted-Python] Sending other things than strings in UDP packets

Andrew Bennetts andrew-twisted at puzzling.org
Wed Oct 6 07:03:56 EDT 2004

On Wed, Oct 06, 2004 at 03:28:36AM -0700, Ibrahim Mubarak wrote:
> Hi,
> I am working on a UDP server. I can send and recieve packets with their data in strings with no
> problems. However, I was wondering if there is a way to populate the data portion of the packet
> with data types other than strings. And to be able to stick a number of values, might be of
> different types, together in the same packet.

UDP is a datagram transport -- it sends packets of some number of bytes with
no guarantees of order or even delivery, beyond that if a packet is
delivered, it will be delivered whole.  All it knows about the payload of
those packets is that they contain bytes.

What those bytes represent is entirely up to the protocol author.  You can
use them to represent ascii strings, unicode characters, a series of 32-bit
integers, or a combination of anything like you like, so long as you can
work out how to encode it as bytes at the sender's end, and decode it at the
receiver's end.  The 'struct' module in the Python standard library can help
a lot here:

> I looked at PB. It is not what I need. I need a low level control on what I add to the packets I
> send and how I can read packets I recieve.

You're right.  PB is a high-level remote object protocol built on TCP.
However, its jelly and banana layers do demonstrate one (moderately complex,
but flexible) approach to serialising arbitrary objects to bytes and back.
It's almost certainly much more than you need for serialising data in UDP
packets, though.


More information about the Twisted-Python mailing list