A Bed Time Programming Test
For my CyberSecurity and Programming sides, I decided to see how quickly OpenAI's Model could generate a good-looking Python Application to Monitor My Network Traffic of any Process. It took 2 tries:
THIS IS FOR A MAC WINDOWS IS DIFFERENT - concerning TCL-TK/Tkinter - Can still be done though.
What I built in 2 seconds...
领英推荐
pip install psutil
brew install tcl-tk
This is the Application name it whatever.py and python3 or python name.py
import psutil
import tkinter as tk
from tkinter import ttk
# Function to update the process list
def update_processes():
processes = []
for proc in psutil.process_iter(['pid', 'name', 'cpu_percent', 'memory_percent']):
try:
# Fetch network connections for the process
connections = proc.connections(kind='inet')
if connections: # Only proceed if there are network connections
for conn in connections:
# Include only connections that are established
if conn.status == psutil.CONN_ESTABLISHED:
processes.append({
'pid': proc.info['pid'],
'name': proc.info['name'],
'cpu_percent': proc.info['cpu_percent'],
'memory_percent': proc.info['memory_percent'],
'local_addr': f"{conn.laddr.ip}:{conn.laddr.port}" if conn.laddr else 'N/A',
'remote_addr': f"{conn.raddr.ip}:{conn.raddr.port}" if conn.raddr else 'N/A',
'status': conn.status
})
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass # Process might have ended or access was denied
return processes
# Function to update the UI
def update_ui():
for i in tree.get_children():
tree.delete(i) # Clear existing entries in the tree
processes = update_processes()
for proc in processes:
tree.insert("", "end", values=(
proc['pid'],
proc['name'],
proc['cpu_percent'],
proc['memory_percent'],
proc['local_addr'],
proc['remote_addr'],
proc['status']
))
# Schedule the update every second
root.after(1000, update_ui)
# Creating the main application window
root = tk.Tk()
root.title("Process Monitor with Network Usage")
# Setting the window size
root.geometry("1000x500")
# Creating the Treeview widget for displaying processes
columns = ("PID", "Name", "CPU Usage (%)", "Memory Usage (%)",
"Local Address", "Remote Address", "Status")
tree = ttk.Treeview(root, columns=columns, show="headings")
# Define headings
tree.heading("PID", text="PID")
tree.heading("Name", text="Name")
tree.heading("CPU Usage (%)", text="CPU Usage (%)")
tree.heading("Memory Usage (%)", text="Memory Usage (%)")
tree.heading("Local Address", text="Local Address")
tree.heading("Remote Address", text="Remote Address")
tree.heading("Status", text="Status")
# Define column widths and anchor (text alignment)
tree.column("PID", width=50, anchor=tk.CENTER)
tree.column("Name", width=200, anchor=tk.W)
tree.column("CPU Usage (%)", width=100, anchor=tk.E)
tree.column("Memory Usage (%)", width=100, anchor=tk.E)
tree.column("Local Address", width=150, anchor=tk.W)
tree.column("Remote Address", width=150, anchor=tk.W)
tree.column("Status", width=100, anchor=tk.CENTER)
# Add lines between columns
style = ttk.Style()
# Raised appearance for headers
style.configure("Treeview.Heading", relief="raised")
style.configure("Treeview", rowheight=25, borderwidth=1,
relief="solid", highlightthickness=1)
# Pack the treeview into the window
tree.pack(fill=tk.BOTH, expand=True)
# Start updating the UI in the main thread
root.after(1000, update_ui)
# Run the Tkinter main loop
root.mainloop()
Promoter
4 个月Thanks for sharing project
?? Your vCISO & Auditor | ISO27001 | ?? Cloudsecurity | Compliance | We automate your security, you focus on your business ?? | Head of Compliance @ PCG (formerly WHYSEC)
4 个月So no more developers needed? ??
Electrical Engineer | Growing Brands with Social Media & Affiliate Marketing | Business Growth Turning Ideas into Sales | Looking to Collaborate with Businesses | Let's Connect
4 个月Awesome share
Empowering Careers & Leaders | Professional Executive Coach | Leadership Trainer | Speaker?Helping You Achieve Promotions, Career Transitions & Leadership Success ? Top Future of Work Leader | Global Recognition Awarded
4 个月Great journey, Aaron Lax. It's so impressive. And inspiring. Congrats!
?????????? ?????? ???????????? | ???????????????? | ????-?????????????? ???? 28 ?????? |???????? ???????????????????? ????????????????????| ?????? ?????? ???????????? ????????????????????
4 个月Thank you for sharing