Skip to content
Permalink
c40547151e
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
37 lines (29 sloc) 1.3 KB
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)
------------------------------------------------------------------------------------------------------------------------------------------------------------------