-
-
Save APie357/64420064755471ec759eb9e46947e160 to your computer and use it in GitHub Desktop.
Simple web server which logs accesses and data [https://www.youtube.com/watch?v=TB3OEG0bKwc]
This file contains hidden or 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
#!/usr/bin/env python3 | |
from flask import Flask, request, send_from_directory | |
from datetime import datetime | |
from user_agents import parse | |
app = Flask(__name__) | |
def get_ip(ip): | |
return ip | |
def log_open_email(): | |
email = request.args.get("e") | |
subject = request.args.get("s") | |
ua = parse(request.headers.get("User-Agent")) | |
ip = get_ip(request.headers.get('X-Forwarded-For', request.remote_addr)) | |
browser = f"{ua.browser.family} {ua.browser.version_string}" | |
os = f"{ua.os.family} {ua.os.version_string}" | |
print(f"{datetime.now()}: '{subject}' opened by {email}, on {browser} on {os} ({ip})") | |
@app.route("/<name>") | |
def get_file(name): | |
log_open_email() | |
return send_from_directory(".", name) | |
@app.after_request | |
def add_header(response): | |
# Prevent caching so the picture always gets reloaded by clients | |
response.cache_control.max_age = 0 | |
return response | |
if __name__ == "__main__": | |
# The request log is annoying so I'm turning it off | |
import logging | |
logging.getLogger("werkzeug").disabled = True | |
app.run(host="0.0.0.0") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you disable the logs you’re basically disabling the whole purpose of this Flask server.