Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending hex packets in python

Tags:

python

hex

packet

How would I send hex data in a packet? I'm trying to copy a packet exactly by using the hex instead of ASCII. All I'm looking for is what the sendto argument would be if, say, the hex I needed to send was 00AD12.

like image 523
Michael Avatar asked Oct 05 '11 23:10

Michael


1 Answers

Use struct to convert between bytes (typically expressed in hexadecimal fashion) and numbers:

>>> import struct
>>> struct.pack('!I', 0xAD12)
b'\x00\x00\xad\x12'

If you have a hex string and want to convert it to bytes, use binascii.unhexlify:

>>> import binascii
>>> binascii.unhexlify('ad12')
b'\xad\x12'
like image 118
phihag Avatar answered Sep 22 '22 19:09

phihag