Data from socket.recv() printing as a mixture of \x and printable ASCII, how to force raw hex?
<p>I have some simple code which is listening to data on a TCP socket:</p>

<pre><code>    data = client_socket.recv(4096)
    print("RECEIVED:",data)
</code></pre>

<p>It works great, but the output looks like this:</p>

<pre><code>RECEIVED: b'\xd8\xff\xfe\x00$\xe3J\xda*\x00
</code></pre>

<p>and so on.</p>

<p>What I want is just the raw hex, like </p>

<pre><code>D8 FF FE 00 24 E3 4A DA 2A 00
</code></pre>

<p>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?</p>

<p>Thanks very much!</p>

<pre><code>import binascii
binascii.hexlify('\xd8\xff\xfe\x00$\xe3J\xda*\x00').upper()
</code></pre>

<p>gets you most of the way ...</p>

python tcp