-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
73 lines (55 loc) · 2 KB
/
app.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from flask import Flask, request
from requests.exceptions import Timeout
from flask_restful import Api, abort
import requests
import socket
app = Flask(__name__)
api = Api(app)
@app.route('/status')
def get_status():
try:
website = request.args.get('url')
timeout = 10 #switch to 0.000001 to test timeout
res = requests.get("https://www." + website, timeout=timeout)
ip = socket.gethostbyname(website)
respose_time = res.elapsed.total_seconds()
status_code = res.status_code
ret = {"status_code" : status_code, "ip" : ip, "resposnse_time" : respose_time}
allow = res.headers.get('allow')
if allow:
ret.update({"Allow" : allow})
connection = res.headers.get('connection')
if connection:
ret.update({"Connection" : connection})
date = res.headers.get('date')
if date:
ret.update({"Date" : date})
expires = res.headers.get('expires')
if expires:
ret.update({"Expires" : expires})
origin = res.headers.get('origin')
if origin:
ret.update({"Origin" : origin})
server = res.headers.get('server')
if server:
ret.update({"Server" : server})
return ret
except Timeout:
#description deprecated in flask 2.x.x?
#abort(408, description="Request timeout! Could not establish connection within 10 seconds.")
return {"message": "Request timeout! Could not establish connection within " + str(timeout) + " seconds."}, 408
except:
return {"message": "Connection error!"}
@app.route('/')
@app.route('/me')
def get_me():
ip = request.remote_addr
ret = {"IP" : ip}
host = request.headers['Host']
if host:
ret.update({"Host" : host})
agent = request.headers['User-Agent']
if agent:
ret.update({"Host" : agent})
return ret
app.run(debug=True)