import socket
HOST = 'localhost'
PORT = 7777
ADDR = (HOST, PORT)
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.bind(ADDR)
if __name__ == '__main__':
try:
while True:
print('Waiting for data ...')
d = server.recvfrom(1024)
received = d[0]
addr = d[1]
print('Received data: ', received)
print('From: ', addr)
msg = input('Enter message to send: ')
server.sendto(msg.encode('utf-8'), addr)
except KeyboardInterrupt:
pass
finally:
server.close()
import socket
HOST = 'localhost'
PORT = 7777
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
if __name__ == '__main__':
try:
while True:
msg = input('Enter message to send: ')
client.sendto(msg.encode('utf-8'), (HOST, PORT))
d = client.recvfrom(1024)
reply = d[0]
addr = d[1]
print('Server reply: ' + reply.decode('utf-8'))
except KeyboardInterrupt:
pass
finally:
client.close()