How does "\x"+* work?
<p>I'm trying to make a filesharing program, so I open the files in readbinary, and read it, establish a connection, and I try to send byte for byte.</p>

<p>How can I send <code>b"\x" + (encoded bytes of the int from dataread[i])</code>?</p>

<p>I always gives me an error, also, if it won't work, how can I read exactly a byte? So that I don't get an int? (like <code>dataread[0]</code>, if the value is <code>"\x01"</code>, I get 1).</p>

<p>My code:</p>

<pre><code>for g in range(len(datar)):
    esc = str(datar[g])
    if len(esc) == 1:
        esc = "0"+esc
    esc = "\x"+bytes(esc,"utf8")
    c.send(esc)
    c.recv(500)
    print(g,"Bytes von",len(datar),"gesendet")
</code></pre>

<p>The <code>'\xhh'</code> notation only works in <em>string or byte literals</em>. If you have an integer, just pass this to a <code>bytes()</code> object in a list:</p>

<pre><code>bytes(dataread)  # if dataread is a list of integers
</code></pre>

<p>or</p>

<pre><code>bytes([dataread])  # if dataread is a single integer
</code></pre>

<p><code>bytes</code> objects are sequences of integer values, each limited to the range 0-255.</p>

<p>To send individual bytes from <code>datar</code>, that translates to:</p>

<pre><code>for byte in datar:
    c.send(bytes([esc]))
    c.recv(500)

print(g,"Bytes von",len(datar),"gesendet")
</code></pre>

python sockets python-3.x byte