Issue
I have a Tkinter text widget and I want to capture and put a server output in it.
Project folder:
project
| main.py
| servers_folder
| | default_server.py
In my main. I'm importing default_server.py
with server = importlib.import_module('default_server.py')
. It has to be another file tho.
main.py code:
def connect(server_name):
for widget in frame.winfo_children():
widget.grid_forget()
def run():
server = importlib.import_module(server_name)
server.Server().start()
command = Text(frame)
command.grid(row=0, column=0)
thread = t.Thread(target=run)
thread.start()
Label(frame, text='Server name:').grid(row=0, column=0)
server_name = Entry(frame)
server_name.grid(row=0, column=1, sticky='NSEW')
Button(frame, text='Run server', command=lambda: connect(server_name.get())).grid(row=1, column=0, columnspan=2)
Server start function:
def start(self):
print('[STARTING] Server is starting...')
self.server.listen()
print(f'[LISTENING] Server is listening on {self.SERVER}')
while True:
conn, addr = self.server.accept()
thread = t.Thread(target=self.handle_client, args=(conn, addr))
thread.start()
Server handle_client function:
def handle_client(self, conn, addr):
print(f'[NEW CONNECTION] {addr} connected.')
print(f'[ACTIVE CONNECTIONS] {t.active_count() - 1}')
connected = True
while connected:
msg_lenght = conn.recv(self.HEADER).decode(self.FORMAT)
if msg_lenght:
msg_lenght = int(msg_lenght)
msg = conn.recv(msg_lenght).decode(self.FORMAT)
if msg == self.DISCONNECT_MESSAGE:
connected = False
print(f'[MESSAGE RECIEVED] FROM: {addr}; Message: {msg}')
conn.send('Message received!'.encode(self.FORMAT))
conn.close()
How can I display server output in my tkinter
text widget?
Thanks for help!
Solution
It would be easily for me to see the full code, but I think you can do something similar to this:
def start(self, msg_callback):
print('[STARTING] Server is starting...')
self.msg_callback = msg_callback
self.server.listen()
print(f'[LISTENING] Server is listening on {self.SERVER}')
while True:
conn, addr = self.server.accept()
thread = t.Thread(target=self.handle_client, args=(conn, addr))
thread.start()
def handle_client(self, conn, addr):
print(f'[NEW CONNECTION] {addr} connected.')
print(f'[ACTIVE CONNECTIONS] {t.active_count() - 1}')
connected = True
while connected:
msg_lenght = conn.recv(self.HEADER).decode(self.FORMAT)
if msg_lenght:
msg_lenght = int(msg_lenght)
msg = conn.recv(msg_lenght).decode(self.FORMAT)
self.msg_callback(msg)
if msg == self.DISCONNECT_MESSAGE:
connected = False
print(f'[MESSAGE RECIEVED] FROM: {addr}; Message: {msg}')
conn.send('Message received!'.encode(self.FORMAT))
conn.close()
def connect(server_name):
for widget in frame.winfo_children():
widget.grid_forget()
command = Text(frame)
command.grid(row=0, column=0)
def msg_callback(msg):
command.delete('1.0', END)
command.insert('1.0', msg)
def run():
server = importlib.import_module(server_name)
server.Server().start(msg_callback)
thread = t.Thread(target=run)
thread.start()
Label(frame, text='Server name:').grid(row=0, column=0)
server_name = Entry(frame)
server_name.grid(row=0, column=1, sticky='NSEW')
Button(frame, text='Run server', command=lambda: connect(server_name.get())).grid(row=1, column=0, columnspan=2)
Answered By - mbostic
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.