python3.5 模拟UDP客户端发送数据报文,报错:TypeError: a bytes-like object is required, not 'str'
#!/usr/bin/env python# -*- coding:utf-8 -*-import sockettarget_host = "127.0.0.1"target_port = 80#建立一个socket对象client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)#发送一些数据response = cli
·
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import socket
target_host = "127.0.0.1"
target_port = 80
#建立一个socket对象
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#发送一些数据
response = client.sendto(("AAABBBCCC"),(target_host,target_port))
print(response)
#接收一些数据
data, addr = client.recvfrom(4096)
print(data)
报错: response = client.sendto(("AAABBBCCC"),(target_host,target_port))
TypeError: a bytes-like object is required, not 'str'
解决方法:
response = client.sendto(("AAABBBCCC").encode(),(target_host.encode(),target_port))
更多推荐
已为社区贡献1条内容
所有评论(0)