I am trying to send Binary data using python raw socket. For that i will do following.
s = '\x01\x00\x12\x59' # some binary data
sock.send(s) # assuming "sock" is a valid, open socket object
i have created a DATAGRAM in HEX in by开发者_如何学Go sniffing a network traffic with wireshark.Which i want to send over the network.This hand made datagram is like
"04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00"
So i wanna convert this above mentioned HEX datagram into the binary format like "\x01\x00\x12\x59". How can i do that?
"04 f8 00 50".replace(' ', '').decode('hex')
Try the following code:
"".join("04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00".split()).decode('hex')
OR:
import binascii
print binascii.unhexlify("".join("04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00".split()))
unhexlify is probably what you're looking for. It doesn't like whitespace, so for your example, unhexlify(data.replace(" ",""))
should work.
精彩评论