Python – How to transfer binary data with Python


I'm working on a client-server program for the first time, and I'm feeling woefully inadequate on where to begin for what I'm doing.

I'm going to use Google Protocol Buffers to transfer binary data between my client and my server. I'm going to be using the Python variant. The basic idea, as I understand, is that the client will serialize the data, send it to the server, which will then deserialize the data.

The problem is, I'm really not sure where to begin for sending binary data to the server. I was hoping it'd be something "simple" like an HTTP request, but I've been searching around Google for ways to transfer binary data and getting lost in the vast multitude of tutorials, guides and documentation. I can't even tell if I'm barking up the wrong tree by investigating HTTP transfers (I was hoping to use it, so I could knock it up a notch to HTTPS if security is necessary). I really don't want to have to go to the level of socket programming, though – I'd like to use the libraries available before turning to that. (I'd also prefer standard Python libraries, though if there's the perfect 3rd party library I'll live.)

So, if anyone has a good starting point (or wants to explain a bit themselves) on how a good way to transfer binary data via Python, I'd be grateful. The server I'm running is currently running Apache with mod_python, by the way.

Best Solution

Any time you're going to move binary data from one system to another there a couple of things to keep in mind.

Different machines store the same information differently. This has implication both in memory and on the network. More info here (

Because you're using python you can cut yourself some slack here (assuming the client and server will both by in python) and just use cPickle to serialize your data. If you really want binary, you're going to have to get comfortable with python's struct module ( And learn how to pack/unpack your data.

I would first start out with simple line-protocol servers until you get past the difficulty of network communication. If you've never done it before it can get confusing very fast. How to issue commands, how to pass data, how to re-sync on errors etc...

If you already know the basics of client/server protocol design, then practice packing and unpacking binary structures on your disk first. I also refer to the RFCs of HTTP and FTP for cases like this.

-------EDIT BASED ON COMMENT-------- Normally this sort of thing is done by sending the server a "header" that contains a checksum for the file as well as the size of the file in bytes. Note that I don't mean an HTTP header, you can customize it however you want. The chain of events needs to go something like this...

CLIENT: "UPLOAD acbd18db4cc2f85cedef654fccc4a4d8 253521"
(server splits the text line to get the command, checksum, and size)
CLIENT: "010101101010101100010101010etc..." (up to 253521 bytes)
(server reasembles all received data into a file, then checksums it to make sure it matches the original)

This is overly simplified, but I hope you can see what I'm talking about here.