#!/usr/bin/env python3 """ This CGI script logs the visitor's IP address and timestamp to a CSV file each time it is accessed. It then returns a 1x1 transparent GIF image. In index.html, this script is included as an image to track visits. """ import datetime import os import sys ip = os.environ.get("REMOTE_ADDR", "unknown") ts = datetime.datetime.now(datetime.timezone.utc).isoformat() #[1] Use absolute path based on user's home directory on Athena script_dir = os.path.dirname(os.path.abspath(__file__)) log_file = os.path.join(script_dir, "visit_log.csv") try: with open(log_file, "a") as f: f.write(f'{ts},{ip}\n') except Exception: pass # Silently fail if logging doesn't work, still return the image #[2] Return HTTP header and 1x1 transparent GIF image (all binary output) # 1x1 transparent GIF gif_data = bytes([ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x80, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x21, 0xF9, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x01, 0x44, 0x00, 0x3B ]) # Write headers and body as binary to avoid encoding issues sys.stdout.buffer.write(b"Content-Type: image/gif\r\n") sys.stdout.buffer.write(b"Content-Length: 43\r\n") sys.stdout.buffer.write(b"\r\n") sys.stdout.buffer.write(gif_data)