-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathflask_demo.py
More file actions
executable file
·63 lines (49 loc) · 1.65 KB
/
Copy pathflask_demo.py
File metadata and controls
executable file
·63 lines (49 loc) · 1.65 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
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
import os
import sys
from flask import (
Flask,
request,
)
# Append this directory to sys.path is not required if the package is already installed
examples_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(examples_dir))
from cloudflare_error_page import ErrorPageParams
from cloudflare_error_page import render as render_cf_error_page
app = Flask(__name__)
@app.route('/')
def index():
params: ErrorPageParams = {
"title": "Internal server error",
"error_code": 500,
"browser_status": {
"status": "ok"
},
"cloudflare_status": {
"status": "error",
"status_text": "Error"
},
"host_status": {
"status": "ok",
"location": "example.com"
},
"error_source": "cloudflare",
"what_happened": "<p>There is an internal server error on Cloudflare\"s network.</p>",
"what_can_i_do": "<p>Please try again in a few minutes.</p>"
}
# Get the real Ray ID from Cloudflare header
ray_id = request.headers.get('Cf-Ray', '')[:16]
# Get the real client ip from Cloudflare header or request.remote_addr
client_ip = request.headers.get('X-Forwarded-For')
if not client_ip:
client_ip = request.remote_addr
params.update({
'ray_id': ray_id,
'client_ip': client_ip,
})
# Render the error page
return render_cf_error_page(params), 500
if __name__ == '__main__':
host = sys.argv[1] if len(sys.argv) > 1 else None
port = int(sys.argv[2]) if len(sys.argv) > 2 else None
app.run(debug=True, host=host, port=port)