python 模拟udp客户端发送报文

import socket

target_host = "xxxxx"
target_port = 7777

# 建立一个socket对象

def send_udp(data):
# 建立一个socket对象
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# 发送一些数据
response = client.sendto((data).encode(),(target_host.encode(),target_port))

print(response)

# 接收一些数据
data, addr = client.recvfrom(4096)

print(data)

if __name__ == '__main__':
data="abcdefg"
send_udp(data)
原文地址:https://www.cnblogs.com/fyangq/p/15084721.html