0

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!

7
  • 2
    I was gonna make a joke about UDP ... but then I didnt know if you would get it Commented Oct 29, 2013 at 1:59
  • 1
    When it comes to getting networking jokes, you could say I'm a bit unreliable! Commented Oct 29, 2013 at 2:05
  • do you have a udp server running already? and you just need the client code? Commented Oct 29, 2013 at 2:14
  • yes, I can start a UDP server and send strings through it. I only need a way to combine multiple strings and send them. One solution turned me on to struct.pack, which let me assemble four objects into a 4-byte string. Perhaps I could do the same with a 7B packer and achieve my goal? Commented Oct 29, 2013 at 2:25
  • whats wrong with str(bytearray(4)) Commented Oct 29, 2013 at 2:32

1 Answer 1

1

is this what you are looking for?

ip_packet = "".join(chr(int(x)) for x in "255.23.0.1".split("."))
sock_client.write(ip_packet)
Sign up to request clarification or add additional context in comments.

2 Comments

This was excellent. I didn't quite understand join before, but with a bit more research I think I know what to do with it. As for combining strings, if you do it by addition, then do you maintain the values of the string in any kind of usable format that allows you to still access the strings?
im not sure what you mean... your original strings do not change

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.