-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
78 lines (60 loc) · 2.19 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
74
75
76
77
78
from flask import Flask
from flask import request
import datetime
import os
app = Flask(__name__)
"""
TO RUN:
1. copy and overwrite contents of nginx.conf to /etc/nginx/nginx.conf
2. move to project directory and run the following command (single line)
uwsgi --http :8000 -s /tmp/test_request_headers.sock --manage-script-name
--mount /test_request_headers=app:app
virtualenv ~/PycharmProjects/test_request_headers/venv/
3. opened port 8000 with ngrok
4. go to generated ngrok url and done. logs will me written to flask_uwsgi.log file in the root directory
"""
@app.route('/')
def hello_world():
try:
if request.method == "GET":
page = 'Received GET request<br>' \
'Request Object type: ' + str(type(request)) + '<br><br>'\
'Request headers contained the following items:<br><br>' \
'%s' % stylized_headers(request.headers)
write_to_log(page)
return page
elif request.method == "POST":
page = 'Received POST request<br>' \
'Request Object type: ' + str(type(request)) + '<br><br>'\
'Request headers contained the following items<br><br>' \
'%s' % stylized_headers(request.headers)
write_to_log(page)
return page
except Exception as e:
write_to_log(e.message)
return 'encountered an error'
def stylized_headers(request_headers):
header_text = ''
try:
for key in request_headers.keys():
value = request_headers.get(key)
header_text += str(key) + ': ' + str(value) + '<br>'
except Exception as e:
header_text = e.message
return header_text
def write_to_log(message):
# check if file exists, it doesnt create it
if not os.path.exists('flask_uwsgi.log'):
f = open('flask_uwsgi.log', "w+")
f.close()
# append message to file
try:
f = open('flask_uwsgi.log', "a+")
current_time = datetime.datetime.now()
f.write('\n')
f.write('[Server]: ' + str(current_time) + ': ' + str(message))
f.close()
except Exception:
print 'error encountered'
if __name__ == '__main__':
app.run()