How do you pass binary data using Python?

I am working on a client-server program for the first time, and I feel extremely inadequate, where to start, what I do.

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

The problem is that I really don't know where to start sending binary data to the server. I was hoping it would be something “simple” like an HTTP request, but I searched Google for ways to transfer binary data and got lost in a huge number of tutorials, manuals and documentation. I can’t even tell if I creep up the wrong tree while exploring HTTP transfers (I was hoping to use it, so I could knock it down to the HTTPS mark if security is needed). I really don't want to go to the socket programming level, although I would like to use the available libraries before addressing this. (I would also prefer the standard Python libraries, although if there was a wonderful third-party library, I would live.)

So, if someone has a good starting point (or wants to explain for themselves) about how a good way to pass binary data through Python, I would be grateful. The server I'm running on is currently running Apache with mod_python, by the way.

+3
source share
4 answers

Every time you are going to move binary data from one system to another, you need to have a few things.

Different machines store the same information in different ways. It matters both in memory and on the network. Read more here ( http://en.wikipedia.org/wiki/Endianness )

python, (, python) cPickle . , struct python (http://docs.python.org/library/struct.html). , / .

, . , . , , ..

/, . RFC HTTP FTP .

------- -------- , "", , . , HTTP-, , . ...

CLIENT: "UPLOAD acbd18db4cc2f85cedef654fccc4a4d8 253521"
SERVER: "OK"
(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)
SERVER: "YEP GOT IT"
CLIENT: "COOL CYA"

, , , .

+4

, , , , .

FAQ, "Twisted - , Python, . -, -, -, .. Twisted , [...]".

, . , .

+3

, , Google, Thrift.

Thrift - . , ++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, #, Cocoa, Smalltalk OCaml.

.

+1

One quick question: why binary? Is the helpful information the most binary, or do you prefer the binary format? If earlier, it is possible to use base64 encoding with JSON or XML; it uses more space (~ 34%) and a bit more processing overhead, but not necessarily enough to make a difference for many use cases.

0
source

Source: https://habr.com/ru/post/1706859/


All Articles