title: How to send utf8-encoded json string by tcp in Python?
body: <p>I'm desesperately trying to send over tcp (Python 2.7) a json string that is utf8-encoded. Here are a few tries and the results. The variable reponse contains the json string I'm trying to send:</p>
  
  <pre><code>reponse = {"candidats":{"P":[{"mentionname":"Beyoncé","guess":[{"name":"BEYONCÉ","score":"1.00","eid":"72437"}]}],"E":[]}}
  </code></pre>
  
  <p>The command 1:</p>
  
  <pre><code>self.request.sendall(json.dumps(reponse+"\n",ensure_ascii=False))
  </code></pre>
  
  <p>results in error:</p>
  
  <pre><code>    'ascii' codec can't encode character u'\xe9' in position 49: ordinal not in range(128)
  </code></pre>
  
  <p>The command 2:</p>
  
  <pre><code>self.request.sendall(json.dumps(reponse+"\n",encoding='utf8')):
  </code></pre>
  
  <p>gives an output at the other end (tcp client) but last character of Beyoncé is not the good one :</p>
  
  <pre><code>   "{\"candidats\":{\"P\"[{\"mentionname\":\"Beyonc\u00e9\",\"guess\":[{\"name\":\"BEYONC\u00c9\",\"score\":\"1.00\",\"eid\":\"72437\"}]}],\"E\":[]}}\n"
  </code></pre>
  
  <p>(message is received in the client with message.decode('UTF-8')). </p>
  
  <p>The command 3:</p>
  
  <pre><code>self.request.sendall(json.dumps(reponse+"\n",ensure_ascii=False,encoding='utf8')):
  </code></pre>
  
  <p>results in error:</p>
  
  <pre><code>    'ascii' codec can't encode character u'\xe9' in position 49: ordinal not in range(128)
  </code></pre>
  
  <p>The command 4:</p>
  
  <pre><code>self.request.sendall(json.dumps(reponse+"\n").encode('utf8')):
  </code></pre>
  
  <p>gives an output at the other end (tcp client) but last character of Beyoncé is not the good one:</p>
  
  <pre><code>    "{\"candidats\":{\"P\":[{\"mentionname\":\"Beyonc\u00e9\",\"guess\":[{\"name\":\"BEYONC\u00c9\",\"score\":\"1.00\",\"eid\":\"72437\"}]}],\"E\":[]}}\n"
  </code></pre>
  
  <p>The command 5:</p>
  
  <pre><code>self.request.sendall(json.dumps(reponse+"\n",ensure_ascii=False).encode('utf8')):
  </code></pre>
  
  <p>gives an output at the other end, last character of Beyoncé is the good one but double quote are escaped:</p>
  
  <pre><code>    "{\"candidats\":{\"P\":[{\"mentionname\":\"Beyoncé\",\"guess\":{\"name\":\"BEYONCÉ\",\"score\":\"1.00\",\"eid\":\"72437\"}]}],\"E\":[]}}\n"
  </code></pre>
  
  <p>Last try is almost the good one, except for those annoying escaped double quotes. I know that this is because string is <a href="https://stackoverflow.com/questions/25242262/python-dump-to-json-adds-additional-double-quotes-and-escaping-of-quotes">double encoded</a> but I have no other choice for the moment to choose this solution and eliminate backslashes in my tcp client code. </p>
  
  <p>Does anybody have a better solution? 
  Any hint is greatly appreciated!
  Regards,
  Patrick </p>