python 2とpython 3 socket sendtoパラメータタイプが異なる問題


python 2.xで問題ありません.
udp_sock_fd.sendto(msg, (target_ip, target_port))

python 3.xでエラーが発生しました.ヒント:
TypeError: a bytes-like object is required, not 'str'

python 3.xとpython 2.xはパラメータタイプ定義が異なり、
python 3.xはbytes、python 2はstrタイプ、
bytesとstrはencode(),decode()で相互変換でき,str--->bytes:encode()でstrをbytesに変換できる.bytes--->str:decode()は、bytesをstrにします.
python 3.xはこう書けばいいです.
udp_sock_fd.sendto(msg.encode(), (target_ip, target_port))