python - Packing structs together -
i have few structs composed python construct(headers , messages). can send each of them tcp socket, failing unite them sending.
#!/usr/bin/env python2.7 import socket import sys construct import * construct.lib import * header = struct( "hdrlength" / int16ul, "hdrcount" / int8ul, ) message = struct( "smth" / int32ul ) hdr = header.build(dict(hdrlength = messages.header.sizeof() + message.sizeof(), hdrcount = 1)) msg = message.build(dict(smth = 32)) sock = socket.socket(socket.af_inet, socket.sock_stream) server_address = ('localhost', 10000) sock.bind(server_address) sock.listen(1)
how can pack variable amount of messages later send bytes in socket?
connection.send(what?)
thanks
construct build
returns bytes
instances.
you can concatenate bytes
instances string, using binary operator +
.
Comments
Post a Comment