Simple 'chat' UDP client and server using Python, can't use `str` as `send()` payload
<p>Iam trying to do a simple 'chat' using UDP in python. I have done both client and server code that is,  </p>

<h1>client</h1>

<pre><code>import socket
fd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM )
udp_ip = '127.0.0.1'
udp_port = 8014
while(True):
    message = input("Client :")
    fd.sendto(message, (udp_ip, udp_port))
    reply = fd.recvfrom(1000)
    print("Server:%s"%(reply))
</code></pre>

<h1>server</h1>

<pre><code>import socket
udp_ip = '127.0.0.1'
udp_port = 8014
fd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
fd.bind((udp_ip,udp_port))
while True:
    r = fd.recvfrom(1000)
    print("client : %s"%(r[0]))
    reply = input('server : ')
    client_address = r[1]
    fd.sendto(reply, client_address)  
</code></pre>

<p>In the client side iam getting  </p>

<pre><code>python client.py 
Client :'haii'
</code></pre>

<p>In the server side iam getting,  </p>

<pre><code> python server.py 
 client : 'haii'
 server : 'hai there'
 Traceback (most recent call last):
 File "server.py", line 12, in &lt;module&gt;
 fd.sendto(reply, client_address)
 Type Error: a bytes-like object is required, not 'str'  
</code></pre>

<p>How to solve this problem? Is anything wrong there?</p>

<p>~                                      </p>

<blockquote>
  <p>How to solve this problem? Is anything wrong there?</p>
</blockquote>

<p>well:</p>

<blockquote>
<pre><code>fd.sendto(reply, client_address)
Type Error: a bytes-like object is required, not 'str'
</code></pre>
</blockquote>

<p>As the error says, you can't directly send a string (Python3 strings are a bit more than just a container of bytes); you must convert it to a <code>bytearray</code> first:</p>

<pre><code> fd.sendto(bytearray(reply,"utf-8"), client_address)
</code></pre>

<p>Notice that you need to specify the encoding; that makes a lot of sense, if you think about how differently characters that aren't usual in English are represented on a byte level. Upside of this conversion is that you can, with unicode, send pretty much anything that is text in any language:</p>

<pre><code>fd.sendto(bytearray("à¤¸à¥à¤à¤¦à¤° à¤­à¤¾à¤·à¤¾","utf-8"), client_address)
</code></pre>

<p>On the other end, you will receive a byte thing, too, and that must be converted to a string first; again, the encoding makes a difference, and you must use the same encoding as to send:</p>

<pre><code>r = fd.recvfrom(1000)
received_msg = str(r, "utf-8")
</code></pre>

<p>Your <code>print("%s" % r )</code> implicitly calls <code>str</code> with default encoding, but that is very likely not a good idea in a network thing. Using utf-8 is pretty much a very good approach to encode strings as bytes. </p>

<p>To give a minimal amount of background: A string should really behave like a string â i.e., a piece of text composed of letters/glyphs/symbols, a representation of <em>text</em>, not some piece of binary memory. Hence, when sending a piece of memory over to someone else, you need to make sure that your text is understood based on  a common representation, in this case, UTF8, on both ends.</p>

python udp