-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
39 lines (29 loc) · 1.16 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import tkinter as tk
import socket
import threading
def start_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('localhost', 2000))
server_socket.listen(1)
print("Server started on port 2000")
while True:
client_socket, client_address = server_socket.accept()
client_data = client_socket.recv(1024).decode()
if client_data:
text_area.insert(tk.END, f"Received: {client_data}\n")
client_socket.sendall(b"OK") # Respond "OK" back to client
client_socket.close()
def run_server():
server_thread = threading.Thread(target=start_server)
server_thread.daemon = True # This will allow the program to exit even if the thread is running
server_thread.start()
def reset_server():
text_area.delete("1.0", tk.END) # Clear the text area
app = tk.Tk()
app.title("Tkinter Server")
text_area = tk.Text(app, width=40, height=10)
text_area.pack()
reset_button = tk.Button(app, text="Reset", command=reset_server) # Add Reset button
reset_button.pack()
run_server() # Automatically start the server when the application starts
app.mainloop()