Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
CloseConnect---Projeckszi/Database tracking log in
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
37 lines (29 sloc)
1.3 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
import json | |
# Initialize login_attempts dictionary | |
login_attempts = {} | |
# Establish connection to SQLite database | |
conn = sqlite3.connect('login_logs.db') | |
c = conn.cursor() | |
# Create login_logs table if not exists | |
c.execute('''CREATE TABLE IF NOT EXISTS login_logs | |
(id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, timestamp TEXT)''') | |
# Callback function for processing messages | |
def process_message(msg): | |
try: | |
payload = json.loads(msg) | |
username = payload.get('username') | |
timestamp = payload.get('timestamp') | |
# Track login attempts | |
login_attempts.setdefault(username, 0) | |
login_attempts[username] += 1 | |
# Insert login record into the database | |
c.execute("INSERT INTO login_logs (username, timestamp) VALUES (?, ?)", (username, timestamp)) | |
conn.commit() | |
print("Login recorded for:", username, "at", timestamp) | |
except Exception as e: | |
print("Error processing message:", e) | |
# Example usage of process_message function with a message | |
example_message = '{"username": "username", "timestamp": "2024-04-04 12:32:00"}' | |
process_message(example_message) | |
------------------------------------------------------------------------------------------------------------------------------------------------------------------ |