Sending binary data over sockets using Python

I am looking for a script for some basic queries using the SPDY protocol . The protocol defines the frames that you send as consisting of binary data with a very specific length and byte order.

I only ever wrote small programs sending strings via sockets (HTTP). How can I, for example, use the header of the SPDY control frame? I tried using the bitstring and numpy library to control the size of all the different sections of the header of the control frame, for example, but nothing works. The current SPDY library for python uses the cython and C data types, and I found that it is very unpredictable. I was wondering how can I create simple queries with pure python or very simple, how can I build something in the same way that the protocol defines and passes it through a socket?

+6
source share
2 answers

Usually the bytearray class will be your friend (if I understood your question correctly). You can send it through a socket:

 my_bytes = bytearray() my_bytes.append(123) my_bytes.append(125) // my_bytes is b'{}' now s.send(my_bytes) 

Follow the protocol specification and create a byte after the byte. This also works when you get data:

 data = s.recv(2048) my_bytes = bytearray(data) 

I know little about the SPDY protocol, but, for example, the control bit is the first bit (not byte) in the message. You can get it from my_bytes via the AND binary, for example:

 control_frame = my_bytes[0] & 128 

this is because 128 is 10000000 in binary format and therefore binary And will give you only the first bit (remember that each byte has 8 bits, so we have 7 zeros).

How everything is done manually. Of course, I suggest using some library, because writing a proper protocol handler will take a lot of time, it may seem to you that it is rather complicated and can be inefficient (depending on your needs, of course).

+15
source

You can also use a struct module , to determine the format of the header with the string and examine it directly.

To create a package:

 fmt = 'BI 4b' your_binary_data = pack(fmt, header_data) sock.sendall(your_binary_data) 

Where fmt indicates the format of the header ('B i 4b' is simple, obviously not working for your SPDY header, for example). Unfortunately, you will have to deal with header fields other than bytes, perhaps by parsing large fragments and then dividing them according to your format.

Also, for parsing the header:

 unpacker = struct.Struct('BI 4b') unpacked_data = unpacker.unpack(s.recv(unpacker.size)) 

unpacked_data will contain a tuple with the analyzed data.

The structure module performs conversions between Python values ​​and C structures, represented as Python strings. I have no guarantee as to the effectiveness of this approach, but it helped me parse the different protocols by simply adjusting the fmt line.

0
source

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


All Articles