In [1]:
# Code attribution: Yiyin Shen, Tyler Caraza-Harter
# Imports
from flask import Flask, request, redirect, Response
import matplotlib.pyplot as plt
import time
import pandas
In [ ]:
# Build a webpage that keeps track of visitors
app = Flask("app")
visitors = []
times = {}
@app.route("/")
def home():
global visitors, times
rate = 3
ip = request.remote_addr
ua = request.user_agent.string
visitors.append([ip, ua])
now = time.time()
if ip in times:
td = now - times[ip]
if td < rate:
return Response("Please come back in " + str(rate - td) + " seconds.", status = 429, headers = {"Retry-After": str(rate)})
else:
times[ip] = now
return "Welcome back, [" + ip + "] " + ua
else:
times[ip] = now
return "Hello, [" + ip + "] " + ua
@app.route("/visitors")
def display():
df = pandas.DataFrame(visitors, columns = ["IP", "User Agent"])
return df.to_html()
app.run(host = "0.0.0.0", debug = False, threaded = False)
* Serving Flask app 'app' * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on all addresses (0.0.0.0) * Running on http://127.0.0.1:5000 * Running on http://192.168.1.87:5000 Press CTRL+C to quit
In [ ]: