title: How do I decode an image/pdf file properly in python3
body: <p>I'm sending a file as follows.</p>
  
  <pre><code>with open(file,'rb') as f:
      rh=f.read()
  </code></pre>
  
  <p>Now to send it I'm using</p>
  
  <pre><code>sock.sendto(rh, (ip,port))
  </code></pre>
  
  <p>While receiving I'm trying to decode this as follows.</p>
  
  <pre><code>dat = sock.recvfrom()
  data=dat.decode('ascii')
  </code></pre>
  
  <p>Then I'm writing this to a file. This works perfectly fine in case of a text file. But If I'm sending and receiving a image/pdf file, I get this error ..</p>
  
  <pre><code>File "code.py", line 16, in &lt;module&gt;
      data=dat.decode('ascii')
  UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 in position 41: ordinal not in range(128)
  </code></pre>
  
  <p>I'm not able to find out what's the problem here. I have also used dat.decode('utf-8') but no use</p>