I need to take specific-sized byte arrays taken from command-line arguments:
rip = bytearray(4) # IP data
rp = bytearray(2) # Port Number
flag = bytearray(1) # Identification Flag
each with different values set to them, and combine them into a single string. This string needs to be able to be sent via UDP socket, and I need to read them again to get information for another process. I want to just send them via this code:
socket.sendto(datastring, '127.0.0.1', LocalPort) #data string contains all bytearray data
and get the data on the other side. I would use a list or a pickle, but neither is allowed for this (and lists wouldn't send anyway).
There seems to be a thousand answers for this question online, but none of them are really clicking with me, or often they seem related but don't help. If anyone can shed some light on this then I would really appreciate it.
EDIT: Here's some code I was turned onto by an associate. It allowed me to turn the IP data in that example code above into a 4-byte string object. I believe I can expand this to cover the 7 bytes of material above.
# create 4-byte array of IP
srip = IP.split('.') #splits IP into string array
GB = struct.Struct("4B") #produces packer for packing IP into a string
rip = GB.pack(int(srip[0]), int(srip[1]), int(srip[2]), int(srip[3])) #creates string to send via UDP
>>>print type(rip)
<type 'str'>
I hope this is on the right track!
str(bytearray(4))