-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
53 lines (44 loc) · 1.8 KB
/
app.py
File metadata and controls
53 lines (44 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from flask import Flask,render_template,request,redirect
import requests as req
from urllib.parse import urlparse
from requests.utils import requote_uri
from form import UrlInput
from datetime import datetime
import os
from os.path import join, dirname
from dotenv import load_dotenv
dotenv_path = join(dirname(__file__), '.env_sample')
load_dotenv(dotenv_path)
SECRET_KEY = os.environ.get("secret_key")
app = Flask(__name__)
app.secret_key = SECRET_KEY
@app.route("/",methods=["GET"])
def home():
formvar = UrlInput()
return render_template("home.html",year=datetime.today().year,form=formvar)
async def tracingfunc(givenURL):
formvar = UrlInput()
headers ={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0"
}
try:
response = req.get(str(givenURL),headers=headers)
responseHistory = [[str(i.url),str(i.status_code)] for i in response.history] + [[str(response.url),str(response.status_code)]] if response.history else [[str(response.url),str(response.status_code)]]
except:
responseHistory =[[str(givenURL),"None"]]
return responseHistory
@app.route("/trace/",methods=["POST","GET"])
async def trace():
formvar=UrlInput()
if request.method == "POST":
givenURL = formvar.Urlinp.data
parsedUrl = urlparse(givenURL)
if (not len(parsedUrl[0])):
givenURL = "http://" + str(givenURL)
encodedURL = requote_uri(str(givenURL))
responseHistory = await tracingfunc(encodedURL)
dictres = {str(i):urldetails for i,urldetails in enumerate(responseHistory)}
return render_template("home.html",form=formvar ,year=datetime.today().year,tracedurls=dictres,encurl=encodedURL)
return redirect("/")
if __name__ == "__main__":
app.run(debug=True)