May-05-2020, 07:05 PM
Hello everyone!
Can someone tell me how can I achieve the functionality shown on this video: https://streamable.com/5c6fz0
I'm stuck somewhere, if anyone could help me I would be grateful. (I used prints just to see if anything will show on the console, they are not needed in the app.)
Basically all it needs to do is send the provided name and the selected request and get the appropriate response back from server. It doesn't have to be exactly like on the video, the functionality is what is important. The animation would be nice to have, of course. All I have to use in this assignment is tkinter, socket and threading.
Thanks in advance.
server.py:
Can someone tell me how can I achieve the functionality shown on this video: https://streamable.com/5c6fz0
I'm stuck somewhere, if anyone could help me I would be grateful. (I used prints just to see if anything will show on the console, they are not needed in the app.)
Basically all it needs to do is send the provided name and the selected request and get the appropriate response back from server. It doesn't have to be exactly like on the video, the functionality is what is important. The animation would be nice to have, of course. All I have to use in this assignment is tkinter, socket and threading.
Thanks in advance.
server.py:
import socket import threading import time from tkinter import * HEADER = 64 FORMAT = 'utf-8' DISCONNECT = 'END' TIME = time.strftime('%H:%M:%S') HOST = socket.gethostbyname(socket.gethostname()) PORT = 12345 ADDR = (HOST, PORT) server = socket.socket() server.bind(ADDR) root = Tk() lb1 = Listbox(root) def client_handler(connection, address): print('[NEW CONNECTION] ' + address) connected = True while connected: msg_length = connection.recv(HEADER).decode(FORMAT) if msg_length: msg_length = int(msg_length) message = connection.recv(msg_length).decode(FORMAT) if message == 'END': connection.send('See you later.'.encode()) connected = False elif message == 'TIME': connection.send(TIME.encode()) print(address + ': ' + message) connection.send('Message received.'.encode(FORMAT)) connection.close() def start_server(): server.listen() print('SERVER: waiting...') while True: conn, addr = server.accept() thread = threading.Thread(target=client_handler, args=(conn, addr)) thread.start() root.mainloop() print('[START] Server started...') start_server()client.py:
from tkinter import * import socket s = socket.socket() HOST = 'localhost' PORT = 12345 def send_to_server(): name = var_entry.get() option = var_radio.get() if option == 1: s.connect((HOST, PORT)) s.send(name.encode() + 'TIME'.encode()) data = s.recv(1024) listbox_responses.insert(data) elif option == 2: s.send(name.encode() + 'END'.encode()) data = s.recv(1024) listbox_responses.insert(data) s.close() root = Tk() # variables var_entry = StringVar() var_radio = IntVar() # canvas c = Canvas(root, bg='yellow', height=150, width=300) coord = 60, 60, 240, 210 arc = c.create_arc(coord, start=0, extent=180, fill='red') c.pack() # name label label_name = Label(root, text='Name', relief=RAISED, bg='blue', fg='white', width=42) label_name.pack() # name entry entry_name = Entry(root, textvariable=var_entry, width=49) entry_name.pack() # radiobuttons radio_time = Radiobutton(root, text='Time', variable=var_radio, value=1) radio_end = Radiobutton(root, text='End', variable=var_radio, value=2) radio_time.pack(anchor=W) radio_end.pack(anchor=W) # button - send request to server button_send = Button(root, text='SEND REQUEST TO SERVER', bg='limegreen', width=40, command=send_to_server) button_send.pack() # label server responses label_responses = Label(root, text='Server responses') label_responses.pack() # listbox responses listbox_responses = Listbox(root, width=45) listbox_responses.pack() root.mainloop()