Σου παραθέτω αυτό που έχω φτιάξει εγώ.
batch filecd C:\python_scripts
python servers_status_client.py
client scriptimport sys, time
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
from tkinter import *
import tkinter as tk
currentDateTime = time.strftime("%d/%m/%Y %H:%M:%S")
PORT_NUMBER = 5000
SIZE = 1024
hostName = gethostbyname('0.0.0.0')
mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )
print ("Client listening on port {0}\n".format(PORT_NUMBER))
while True:
(data, addr) = mySocket.recvfrom(SIZE)
strMessage = '{} \n\n ο server με ip {} \n δεν απαντά.'.format(currentDateTime, data)
NORM_FONT = ("Helvetica", 11)
popup = tk.Tk()
popup.wm_title("ΠΡΟΣΟΧΗ!")
label = tk.Label(popup, text = strMessage, font = NORM_FONT)
label.pack(side = "top", fill = "x", pady = 10)
B1 = tk.Button(popup, text="Κλείσιμο", command = popup.destroy)
B1.pack()
# popup.geometry("400x150") #disabled for auto fit
popup.wm_attributes("-topmost", 1) #this directive is to keep popup window always on top of all other windows
popup.mainloop()
sys.exit()
server script
#Import libraries
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
#
# send message to client pc's
#
def sendMessageToClients(strMessage, chHost):
try:
# CLIENT_IP_LIST = ('192.0.0.216', '192.0.0.150', '192.0.0.15')
CLIENT_IP_LIST = ('192.0.0.216',)
PORT_NUMBER = 5000
SIZE = 1024
for CLIENT_IP in CLIENT_IP_LIST:
mySocket = socket(AF_INET, SOCK_DGRAM)
for i in range(0, 1):
mySocket.sendto(bytes(strMessage, encoding = "iso-8859-7"),(CLIENT_IP, PORT_NUMBER) )
except Exception as sendMessageException:
print(sendMessageException)