Hubert S. Cumberdale Hubert S. Cumberdale - 1 year ago 93
Python Question

Data from socket.recv() printing as a mixture of \x and printable ASCII, how to force raw hex?

I have some simple code which is listening to data on a TCP socket:

data = client_socket.recv(4096)

It works great, but the output looks like this:

RECEIVED: b'\xd8\xff\xfe\x00$\xe3J\xda*\x00

and so on.

What I want is just the raw hex, like

D8 FF FE 00 24 E3 4A DA 2A 00

My end goal is to put the raw bytes straight into a file, but I do want to print them first. How do I print them as I desire, and how do I put them into a file? Maybe I do not even need to manipulate 'data' to put it in a file?

Thanks very much!

Answer Source
import binascii

gets you most of the way ...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download