def decode(self, *args, **kwargs): # real signature unknown"""Decode the bytes using the codec registered for encoding.使用注册用于编码的编解码器解码字节。encodingThe encoding with which to decode the bytes.用于解码字节的编码。errorsThe error handling scheme to use for the handling of decoding errors.The default is 'strict' meaning that decoding errors raise aUnicodeDecodeError. Other possible values are 'ignore' and 'replace'as well as any other name registered with codecs.register_error thatcan handle UnicodeDecodeErrors.用于处理解码错误的错误处理方案。默认值为“严格”,这意味着解码错误会引起UnicodeDecodeError。 其他可能的值是'ignore'和'replace'以及在codecs.register_error中注册的任何其他可处理UnicodeDecodeErrors的名称。"""pass
示例:
在UDP客户端上,使用decode()函数对服务端返回的bytes字符进行解码后打印输出
# -*- coding: utf-8 -*-"""@File : 191226_基于UDP协议的socket_client端.py@Time : /12/26 23:32@Author : Dontla@Email : sxana@@Software: PyCharm"""import socketip_port = ('192.168.1.49', 9000)BUFSIZE = 1024udp_server_client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)while True:msg = input('>>: ').strip()if not msg:continueudp_server_client.sendto(msg.encode('utf-8'), ip_port)back_msg, addr = udp_server_client.recvfrom(BUFSIZE)print(back_msg.decode('utf-8'), addr)
结果:
D:\1031_tensorflow_yolov3\python\python.exe D:/1221_network_programming/network_programming/基于UDP协议的socket/191226_基于UDP协议的socket_client端.py>>: 莫哈皮B'\XE8\X8E\XAB\XE5\X93\X88\XE7\X9A\XAE' ('192.168.1.49', 9000)>>: 方法B'\XE6\X96\XB9\XE6\XB3\X95' ('192.168.1.49', 9000)>>: ddB'DD' ('192.168.1.49', 9000)>>: >>: aA ('192.168.1.49', 9000)>>: 你你 ('192.168.1.49', 9000)>>: 你你 ('192.168.1.49', 9000)>>: 哈珀哈珀 ('192.168.1.49', 9000)>>: 莫哈皮莫哈皮 ('192.168.1.49', 9000)>>:
为啥上面一开始打印出来的是字节码??