views:

103

answers:

2

Basically i want to be able to get a 32bit int and attach its binary to the binary of a string. E.g. (IM going to use 8bit instead of 32bit) i want 255 + hi 11111111 + 0110100001101001 = 111111110110100001101001 So the int holds its binary value,i dont care how it comes out i just want it to be able to send the data over a socket.

(This is all over websockets and the new sec-websocket-key's to stop hacking, if anyone just knows how to do the websocket handshake that would be just as nice)

Thankyou ! I have been trying on this for days and im not one to come to this type of website to get the answer

EDIT

Ive been ask to give more info so her is the full deal. I have connected to the user of a stream port, he has sent me headers now i need to reply to to complete the connection. The import data is

Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5"Random string following rules"(i will call this sk1)

Sec-WebSocket-Key2: 12998 5 Y3 1 .P00 "Random string following rules"(i will call this sk2)

^n:ds[4U "Random string following rules"(i will call this sk3)

1) int1 = compress the numbers into sk1 and divid them by the amount of spaces in sk1

2) int2 = compress the numbers into sk2 and divid them by the amount of spaces in sk2

3) fullapend = Add append the bytes of int2 to int1 then append the bytes in sk3

4) Finally MD5 digest fullapend

5) Send the final result to the host along with some other headers and if they match up the connection holds open

That is everything that needs to happen and i have not got a clue how to do it

Finished !

Well basic both answers was right and i would like to apologise if i seemed a bit rude, i didnt know the \x was a (something) meaning binary. but that worked a treat. Once i have the finished function to connect send etc... i will post it on here and else where for anyone else thats stuck, again thankyou !

+3  A: 

Something like

struct.pack("!i%ds" % len(your_string), your_int, your_string)

should do pretty much what you want !

mripard
Thats good but in the live python terminal it says the data type is a string if i send that over the internet/socket/tcp what ever its called wont it send send \x04\x84 etc... no its actaul value.
kizzie33
Well actually struct.pack returns a string containing the binary value of the given elements, and I've used it a lot of time to send data through a socket. And sending \x04\x84 isn't quite a problem, or is it ?
mripard
@user416186: What version of python are you using? `"what ever its called"` is typically an important detail to programmers. You might want to expand your question with what you've got so far.
MattH
+2  A: 

Is this what you're looking for? Don't know whether you're after a hexdigest or a digest, and I couldn't tell where the keys started and stopped, which is a shame as they are whitespace sensitive.

Also in your update you said "compress" numbers when I think you meant "concatenate". I think the resulting keys are supposed to be bigendian, which is what I've done.

>>> import struct
>>> def processKey(data):
...     num = int("".join([x for x in data if x.isdigit()]))
...     spaces = data.count(' ')
...     return num / spaces
...
>>> key1 = '4 @1 46546xW%0l 1 5'
>>> key2 = '12998 5 Y3 1 .P00 '
>>> sk1 = processKey(key1)
>>> sk2 = processKey(key2)
>>> sk1
1036636503L
>>> sk2
259970620
>>> sk3 = "^n:ds[4U"
>>> fullappend = struct.pack('>ii%ds' % len(sk3),sk1,sk2,sk3)
>>> fullappend
'=\xc9\xd1W\x0f~\xd6<^n:ds[4U'
>>> len(fullappend)
16
>>> from hashlib import md5
>>> md5(fullappend).hexdigest()
'fd028b6b39ceb8e37f09b8e45556bbc4'
>>> md5(fullappend).digest()
'\xfd\x02\x8bk9\xce\xb8\xe3\x7f\t\xb8\xe4UV\xbb\xc4'
MattH