用套接字发送UTF-8 [英] Sending UTF-8 with sockets

查看:186
本文介绍了用套接字发送UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在python中设置一个聊天程序。一切都正常,直到我
发送一个包含导致程序崩溃的非ascii字符的字符串。该字符串从wx.TestCtrl

中读取


  • 如何通过套接字发送UTF-8编码字符串?


  • 为什么程序开始时没有问题?我已经将编码设置为UTF-8
    ,所以不会导致程序崩溃?




这是错误:

 追溯(最近的最后一次呼叫):
文件./client.py ,line 180,in sendMess
outSock.sendto(s,self.serveraddr)
UnicodeEncodeError:'ascii'编解码器不能在位置26中编码字符u'\xf6':
序号不在范围(128)

这是我如何创建套接字并尝试发送消息:

  outSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
....
outSock.sendto(s,self.serveraddr)


解决方案

套接字上的Python 2, socket.sendto 需要一个plain字符串,而不是 unicode 对象。因此,您必须编码它,比如使用UTF-8:

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

同样,当您 recvfrom (或类似)在另一端,你需要转换回一个Unicode对象:

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

(在Python 3中, code> bytes ,这使得需要在它和 unicode 之间进行转换更明确。)


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 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.)

这篇关于用套接字发送UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆