Convert an int and bytearray into a ByteString Python struct Socket -
i need send 1 int und 1 bytearray(200) through socket server. socket.send() function accpets 1 string need int , bytearray bytes in 1 string. tryed convert both string struct.pack(), working int not bytearray.
s = socket.socket(socket.af_inet, socket.sock_stream) s.connect((tcp_ip, tcp_port)) print "connected to: ", s.getpeername() #trying put int , bytearray 1 string a= 02 # int b= bytearray(200) #bytearray c = struct.pack("b", a) c += b s.send(c) print s.recv(1024)
concatenate them:
>>> import struct >>> struct.pack('<l', 1234) + bytearray([0]*10) b'\xd2\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
or specify bytearray:
>>> struct.pack('<l10s', 1234, bytearray([0]*10)) # in python 3.x # struct.pack('<l10s', 1234, bytes(bytearray([0]*10))) # in python 2.x b'\xd2\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Comments
Post a Comment