write a python program for monitoring people accessing unprofessional sites
- User
write a python program for monitoring people accessing unprofessional sites
- Assistant
write a python program for monitoring people accessing unprofessional sites
Monitoring web access can be an important tool for ensuring a safe and professional online environment, whether in a workplace, educational institution, or at home. Below is a basic Python script that can help you log URLs accessed on a Windows machine by monitoring the browser history. Please use this responsibly and ensure you comply with all applicable laws and regulations regarding privacy and data monitoring in your jurisdiction.
The following script will monitor the browsing history of common browsers (like Google Chrome and Mozilla Firefox) by accessing their history databases. It logs the URLs accessed to a local file for further review.
sqlalchemy and pandas libraries. You can install them using pip:
pip install sqlalchemy pandas
import os
import time
import sqlite3
import pandas as pd
from datetime import datetime
# Paths to browser history databases
HISTORY_PATHS = {
'chrome': os.path.expandvars(r'%LOCALAPPDATA%\Google\Chrome\User Data\Default\History'),
'firefox': os.path.expanduser('~') + r'\AppData\Roaming\Mozilla\Firefox\Profiles',
}
# Log file
LOG_FILE = 'access_log.csv'
def get_chrome_history(history_path):
try:
conn = sqlite3.connect(history_path)
cursor = conn.cursor()
query = "SELECT url, title, last_visit_time FROM urls"
cursor.execute(query)
data = cursor.fetchall()
column_names = [description[0] for description in cursor.description]
df = pd.DataFrame(data, columns=column_names)
conn.close()
# Convert Chrome's timestamp to readable format
df['last_visit_time'] = pd.to_datetime(df['last_visit_time'] / 1e6 - 11644473600, unit='s')
return df[['url', 'title', 'last_visit_time']]
except Exception as e:
print(f"Error accessing Chrome history: {e}")
return pd.DataFrame()
def get_firefox_history(profile_path):
history_dbs = [os.path.join(profile_path, p, 'places.sqlite') for p in os.listdir(profile_path) if os.path.isdir(os.path.join(profile_path, p))]
df_total = pd.DataFrame()
for db in history_dbs:
try:
conn = sqlite3.connect(db)
cursor = conn.cursor()
query = """
SELECT moz_places.url, moz_places.title, moz_historyvisits.visit_date
FROM moz_places
JOIN moz_historyvisits ON moz_places.id = moz_historyvisits.place_id
"""
cursor.execute(query)
data = cursor.fetchall()
column_names = [description[0] for description in cursor.description]
df = pd.DataFrame(data, columns=column_names)
conn.close()
# Convert Firefox's timestamp to readable format
df['visit_date'] = pd.to_datetime(df['visit_date']/1000000, unit='s')
df_total = pd.concat([df_total, df], ignore_index=True)
except Exception as e:
print(f"Error accessing Firefox history: {e}")
return df_total[['url', 'title', 'visit_date']]
def log_history():
chrome_history = get_chrome_history(HISTORY_PATHS['chrome'])
firefox_profiles = HISTORY_PATHS['firefox']
firefox_history = get_firefox_history(firefox_profiles)
combined_history = pd.concat([chrome_history, firefox_history], ignore_index=True)
combined_history = combined_history.sort_values(by='last_visit_time' if 'last_visit_time' in combined_history.columns else 'visit_date', ascending=False)
# Remove duplicates
combined_history.drop_duplicates(subset=['url'], keep='last', inplace=True)
# Save to CSV
combined_history.to_csv(LOG_FILE, index=False)
print(f"History logged at {datetime.now()}")
def monitor(interval=300):
"""
Monitor browser history every 'interval' seconds.
"""
print("Starting to monitor browser history...")
while True:
log_history()
time.sleep(interval)
if __name__ == "__main__":
monitor_interval = 300 # Monitor every 5 minutes
monitor(monitor_interval)
Browser History Access:
History SQLite database located in the user's local app data.places.sqlite file within each user profile. The script iterates through all profiles to gather history.Data Extraction:
Logging:
access_log.csv file.Monitoring Loop:
monitor function runs indefinitely, logging browser history at specified intervals (default is every 5 minutes).monitor_interval variable to set how frequently the history is logged (in seconds).While a Python script can provide basic monitoring capabilities, there are dedicated software solutions that offer more comprehensive features, such as:
These solutions often provide more robust features, better security, user-friendly interfaces, and support compared to a custom script.
Monitoring online activity carries significant responsibilities. Ensure that:
If you're unfamiliar with handling user data securely or have specific requirements, consulting with an IT professional or legal advisor is recommended.
Not yet included in a published release.