I'm tring to setup a little chat program in python. Everything was working fine until I sent a string containing a non ascii character that caused the program to crash. The string are read from a wx.TestCtrl
How can I send a string with UTF-8 encoding over sockets?
Why does the program work without problems at the start? I have set the encoding to UTF-8 so wouldn't all character cause the program to crash?
Here is the error:
Traceback (most recent call last):
File "./client.py", line 180, in sendMess
outSock.sendto(s,self.serveraddr)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 26:
ordinal not in range(128)
Here is how I create the socket and try to send the message:
outSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
....
outSock.sendto(s,self.serveraddr)
In Python 2,
socket.sendto
on a socket takes a "plain" string, not aunicode
object. Therefore you must encode it, say using UTF-8:Similarly, when you
recvfrom
(or similar) at the other end, you'll need to convert back to a Unicode object:(In Python 3, you'll be working with
bytes
, which makes the need to convert between it andunicode
more explicit.)