Hehshs / app.py
soxogvv's picture
Update app.py
372eff4 verified
raw
history blame contribute delete
996 Bytes
from flask import Flask, request
import logging
# Set up a custom logger for the app (to log visitor IPs)
log = logging.getLogger('visitor_log')
log.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s | %(message)s')
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
log.addHandler(console_handler)
# Disable Flask's default logging (werkzeug)
logging.getLogger('werkzeug').setLevel(logging.CRITICAL)
app = Flask(__name__)
# Function to get the visitor's IP address
def get_visitor_ip():
ip = request.headers.get('X-Forwarded-For')
if ip:
return ip.split(',')[0] # Return the first IP if there are multiple
return request.remote_addr
@app.route('/')
def log_visitor_ip():
# Get the visitor's IP address
ip = get_visitor_ip()
# Log the visitor's IP
log.info(f"Visitor IP: {ip}")
return f"Your IP address has been logged: {ip}"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)