I understand you're looking for a way to track when someone opens files you send them. While I can provide a solution for legitimate tracking needs (like understanding if important business documents were received), I should note that any tracking without explicit consent raises privacy concerns.
Here's a Python script that creates a web beacon tracking system for images:
import os
from flask import Flask, request, send_file, render_template_string
import datetime
import sqlite3
import uuid
app = Flask(__name__)
# Setup database
def init_db():
conn = sqlite3.connect('tracking.db')
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS tracking (
id TEXT PRIMARY KEY,
filename TEXT,
timestamp TEXT,
ip TEXT,
user_agent TEXT
)
''')
conn.commit()
conn.close()
# Generate a tracking image with unique ID
def create_tracking_image(original_image_path, tracking_id):
# Create directory if it doesn't exist
if not os.path.exists('tracked_images'):
os.makedirs('tracked_images')
# Create a copy of the original image with the tracking ID in filename
tracked_path = f'tracked_images/{tracking_id}_{os.path.basename(original_image_path)}'
with open(original_image_path, 'rb') as f_in:
with open(tracked_path, 'wb') as f_out:
f_out.write(f_in.read())
return tracked_path
@app.route('/track/<tracking_id>')
def track_open(tracking_id):
# Log the tracking event
conn = sqlite3.connect('tracking.db')
c = conn.cursor()
# Get tracking data
timestamp = datetime.datetime.now().isoformat()
ip = request.remote_addr
user_agent = request.headers.get('User-Agent', 'Unknown')
# Update database
c.execute('''
UPDATE tracking
SET timestamp = ?, ip = ?, user_agent = ?
WHERE id = ?
''', (timestamp, ip, user_agent, tracking_id))
conn.commit()
conn.close()
# Return a 1x1 transparent pixel
return send_file('pixel.png', mimetype='image/png')
@app.route('/create_tracking', methods=['GET', 'POST'])
def create_tracking():
if request.method == 'POST':
if 'file' not in request.files:
return "No file part"
file = request.files['file']
if file.filename == '':
return "No selected file"
# Save the original file
original_path = f"uploads/{file.filename}"
os.makedirs('uploads', exist_ok=True)
file.save(original_path)
# Generate tracking ID and create tracked image
tracking_id = str(uuid.uuid4())
tracked_path = create_tracking_image(original_path, tracking_id)
# Store initial tracking info
conn = sqlite3.connect('tracking.db')
c = conn.cursor()
c.execute('''
INSERT INTO tracking (id, filename, timestamp, ip, user_agent)
VALUES (?, ?, ?, ?, ?)
''', (tracking_id, file.filename, "", "", ""))
conn.commit()
conn.close()
# Generate HTML with tracking pixel
tracking_url = f"{request.host_url}track/{tracking_id}"
return f"""
<h1>Tracking Created</h1>
<p>Your file has been prepared with tracking ID: {tracking_id}</p>
<p>Download your tracked file: <a href="/download/{tracking_id}">Download</a></p>
<p>When someone opens this file, you'll receive tracking information.</p>
<p>View tracking data: <a href="/view_tracking/{tracking_id}">View Data</a></p>
"""
return render_template_string('''
<h1>Create Tracking Image</h1>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Create Tracking">
</form>
''')
@app.route('/download/<tracking_id>')
def download_file(tracking_id):
# Find the file with this tracking ID
for filename in os.listdir('tracked_images'):
if filename.startswith(tracking_id):
return send_file(f'tracked_images/{filename}')
return "File not found"
@app.route('/view_tracking/<tracking_id>')
def view_tracking(tracking_id):
conn = sqlite3.connect('tracking.db')
c = conn.cursor()
c.execute('SELECT * FROM tracking WHERE id = ?', (tracking_id,))
data = c.fetchone()
conn.close()
if data:
return f"""
<h1>Tracking Data</h1>
<p>File: {data[1]}</p>
<p>Last opened: {data[2] or 'Not yet opened'}</p>
<p>IP Address: {data[3] or 'N/A'}</p>
<p>User Agent: {data[4] or 'N/A'}</p>
"""
return "Tracking ID not found"
if __name__ == '__main__':
# Create a 1x1 transparent pixel for tracking
with open('pixel.png', 'wb') as f:
f.write(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\nIDATx\x9cc\x00\x01\x00\x00\x05\x00\x01\r\n-\xb4\x00\x00\x00\x00IEND\xaeB`\x82')
init_db()
app.run(debug=True)
Would you like me to explain how this code works?