How to read from a socket in Python?
> @GreyCat thank you for all the comments so far. I'm currently using the following (old style syntax) and it works fine:
> 
> ```python
> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> server_address = (host, port)
> sock.connect(server_address)
> stream = KaitaiStream(BytesIO(sock.recv(4)))
> obj = GeneratedProtocol(stream)
> print(obj.attribute)
> ```
> 
> I was just wondering if there is a smarter way to specify how many bytes should be read from the socket (`sock.recv(max_bytes)`)? I was not able to figure out the size based on the kaitai_struct object. Especially reading arrays or other dynamically sized objects seem to be difficult to read (always reading the length first and then element by element?). Maybe there is a cleaner and less error prone way to do this?

_Originally posted by @fabolhak in https://github.com/kaitai-io/kaitai_struct/issues/162#issuecomment-2229131081_