Hi all,
I am trying to send Binary data using In python raw socketusing socket.send().
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 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".

Use functions hexlify() and unhexlify() in module binascii for this

>>> from binascii import hexlify, unhexlify
>>> s = '\x01\x00\x12\x59'
>>> h = hexlify(s)
>>> print h
01001259
>>> assert unhexlify(h) == s
>>>

thanks a lot for your reply..But one thing i wanna know what abou converting "04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00" hex in a format like this "\x01\x00\x12\x59"

thanks a lot for your reply..But one thing i wanna know what abou converting "04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00" hex in a format like this "\x01\x00\x12\x59"

Remove white space and use unhexlify()

s = "04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00"
ss = s.replace(" ", "")
binary = unhexlify(ss)
commented: 3 +3

Thanks again.I think its working but i am getting some special characters The printed code does not look like this one..-->> "\x01\x00\x12\x59"..If the printed special chars are equivalent to binary data can i send that through socket like i have mentioned in the main post.like

from binascii import hexlify, unhexlify
import socket
s = "04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00"
ss = s.replace(" ", "")
binary = unhexlify(ss)
print binary
sock.send(binary)

Or do this ...

s = "04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00"

hs = ""
for b in s.split():
    hs += "\\x%02x" % int(b, 16)

print(s)
print(hs)

'''
04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00
\x04\xf8\x00\x50\x4f\x30\xfb\x47\x28\x62\xa7\x6d\x50\x02\x02\x00\xd2\x7f\x00\x00
'''

Thanks again.I think its working but i am getting some special characters The printed code does not look like this one..-->> "\x01\x00\x12\x59"..If the printed special chars are equivalent to binary data can i send that through socket like i have mentioned in the main post.like

from binascii import hexlify, unhexlify
import socket
s = "04 f8 00 50 4f 30 fb 47 28 62 a7 6d 50 02 02 00 d2 7f 00 00"
ss = s.replace(" ", "")
binary = unhexlify(ss)
print binary
sock.send(binary)

To print binary strings, you should print their repr():

>>> print repr(binary)
'\x04\xf8\x00PO0\xfbG(b\xa7mP\x02\x02\x00\xd2\x7f\x00\x00'

Otherwise, yes, you can send your binary string in the socket; hexlify() and unhexlify() are robust functions.

Thnx a lot buddy..!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.