server
#!/usr/bin/env python # -*- coding: UTF-8 -*- import socket def main(): host = "127.0.0.1" port = 5000 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind((host, port)) print("Server started") while True: data, addr = s.recvfrom(1024) print("Message from: " + str(addr)) print("From connected user: " + str(data)) data = str(data).upper() print("Sending: " + str(data)) s.sendto(bytes(data, "utf-8"), addr) s.close() if __name__ == '__main__': print("socket server running...") main()
client
#!/usr/bin/env python # -*- coding: UTF-8 -*- import socket def main(): host = "127.0.0.1" port = 5001 server = ("127.0.0.1", 5000) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind((host, port)) message = input("-->") while message != "q": s.sendto(bytes(message, "utf-8"), server) data, addr = s.recvfrom(1024) print("Received from server: " + str(data)) message = input("-->") s.close() if __name__ == '__main__': main()