-
Notifications
You must be signed in to change notification settings - Fork 0
/
tk_9.py
52 lines (41 loc) · 1.53 KB
/
tk_9.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
40
41
42
43
44
45
46
47
48
49
50
51
52
import threading
from subprocess import Popen, PIPE
from time import sleep
import tkinter as tk
from tkinter import *
PROCESS = ['netstat','1']
class Console(tk.Frame):
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master, *args, **kwargs)
self.text = tk.Text(self, undo=True)
self.text.pack(expand=True, fill="both")
# run process in a thread to avoid blocking gui
t = threading.Thread(target=self.execute)
t.start()
def display_text(self, p):
display = ''
lines_iterator = iter(p.stdout.readline, b"")
for line in lines_iterator:
if 'Active' in line:
self.text.delete('1.0', END)
self.text.insert(INSERT, display)
display = ''
display = display + line
def display_text2(self, p):
while p.poll() is None:
line = p.stdout.readline()
if line != '':
if 'Active' in line:
self.text.delete('1.0', END)
self.text.insert(END, line)
p.stdout.flush()
def execute(self):
p = Popen(PROCESS, universal_newlines=True,
stdout=PIPE, stderr=PIPE)
print('process created with pid: {}'.format(p.pid))
self.display_text(p)
if __name__ == "__main__":
root = tk.Tk()
root.title("netstat 1")
Console(root).pack(expand=True, fill="both")
root.mainloop()