#发送数据
import socket
def main():
#udp套接字对象
udp_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
data = input("请输入要发送的数据")
#发送数据 字符串前面是b
#udp_socket.sendto(b"1234",("192.168.x.x", xxxx))
udp_socket.sendto(data.encode("utf-8"), ("192.168.x.xxx", 8080))
udp_socket.close()
if __name__ == '__main__':
main()
#循环发送
import socket
def send(data):
udp_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
udp_socket.sendto(data.encode("utf-8"),("192.168.x.xxx", xxxx))
udp_socket.close()
if __name__ == '__main__':
while True:
data = input("input:")
if data == "exit":
break
send(data)
# 先绑定端口再发送数据
import socket
def main():
#udp套接字对象
udp_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
udp_socket.bind(("", 7890)) # 绑定端口
while True:
data = input("请输入要发送的数据")
#发送数据 字符串前面是b
#udp_socket.sendto(b"1234",("192.168.1.114", 8080))
udp_socket.sendto(data.encode("utf-8"), ("192.168.1.114", 8080))
udp_socket.close()
if __name__ == '__main__':
main()