views:

169

answers:

4

I'm learning socket programming (in python) and I was wondering what the best/typical way of encapsulating data is? My packets will be used to issue run, stop, configure, etc. commands on the receiving side. Is it helpful to use JSON or just straight text?

+1  A: 

I suggest you use a fixed, or mostly fixed format, as this make things easier.
By then using features such as the standard library's struct.Struct, with its pack() and umpack() methods, or possibly a slightly more featured pacakges such as Construct, you should have much of the parsing work done for you ;-)

mjv
A: 

If you're developing something as a learning exercise you might find it best to go with a structured text (ie. human readable and human writable) format.

An example would be to use a fixed number of fields per command, fixed width text fields and/or easily parsable field delimiters.

Generally text is less efficient in terms of packet size, but it does have the benefits that you can read it easily if you do a packet capture (eg. using wireshark) or if you want to use telnet to mimic a client.

And if this is only a learning exercise then ease of debugging is a significant issue.

Andrew Edgecombe
A: 

Take a look at how scapy (an awesome Python packet manipulation library) implements it. Looks like that have a handful of fields.

John Paulett
+1  A: 

I suggest plain text to begin with - it is easier to debug. The format that your text takes depends on what you're doing, how many commands, arguments, etc. Have you fleshed out how your commands will look? Once you figure out what that looks like it'll likely suggest a format all on its own.

Are you using TCP or UDP? TCP is easy since it is a stream, but if you're using UDP keep in mind the maximum size of UDP packets and thus how big your message can be.

Steven Adams