main/03_networking/udp_client.py

20 lines
541 B
Python

import socket
def main():
HOST = '127.0.0.1'
PORT = 10001
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
messages = ["Привет UDP!", "Тест", "exit"]
for msg in messages:
print(f"Отправка: {msg}")
s.sendto(msg.encode('utf-8'), (HOST, PORT))
if msg.lower() == 'exit':
break
data, _ = s.recvfrom(1024)
print(f"Ответ: {data.decode()}")
if __name__ == '__main__':
main()