Sending UTF-8 with sockets

2019-04-16 16:34发布

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)

1条回答
我只想做你的唯一
2楼-- · 2019-04-16 17:05

In Python 2, socket.sendto on a socket takes a "plain" string, not a unicode object. Therefore you must encode it, say using UTF-8:

outSock.sendto(s.encode('utf-8'), self.serveraddr)

Similarly, when you recvfrom (or similar) at the other end, you'll need to convert back to a Unicode object:

unicode_string = s.decode('utf-8')

(In Python 3, you'll be working with bytes, which makes the need to convert between it and unicode more explicit.)

查看更多
登录 后发表回答