-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.py
140 lines (110 loc) · 3.85 KB
/
web.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from functools import wraps
from flask import request, Response, Flask, make_response ,json
from flask import render_template
from classes.logger import logger
#from flask.ext.uwsgi_websocket import GeventWebSocket
from flask.ext.uwsgi_websocket import WebSocket
#from flask_uwsgi_websocket.websocket import WebSocket
########################################################################################################################
from modules.data_module import data_module
app = Flask(__name__)
#app.debug = True
#app.config['DEBUG'] = True
'''
this is for loadimg flask admin like a blueprint
'''
#Admin
from flask.ext.admin import Admin
admin = Admin(app)
#add admin
import modules.admin_module
'''
WEBSOCKET
'''
ws = WebSocket(app)
#ws = GeventWebSocket(app)
########################################################################################################################
app.register_blueprint(data_module)
########################################################################################################################
@app.route("/")
def hello():
return render_template('under_construction.html')
#example
@app.route('/hello/')
@app.route('/hello/<name>')
def hello1(name=None):
return render_template('name_template.html', name=name)
########################################################################################################################
#HEADERS
def add_response_headers(headers={}):
"""This decorator adds the headers passed in to the response"""
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
resp = make_response(f(*args, **kwargs))
h = resp.headers
for header, value in headers.items():
h[header] = value
return resp
return decorated_function
return decorator
def appId(f):
"""This decorator passes X-Robots-Tag: noindex"""
@wraps(f)
@add_response_headers({'X-IBS-APP_ID': 'akldqwdj283742j9834h2d'})
def decorated_function(*args, **kwargs):
return f(*args, **kwargs)
return decorated_function
@app.route('/h')
@add_response_headers({'X-IBS-APP_ID': 'akldqwdj283742j9834h2d'})
def not_indexed():
"""
This page will be served with X-Robots-Tag: noindex
in the response headers
"""
return "Check my headers!"
@app.route('/headers')
def headers():
api_id = request.headers.get('X-IBS-API-ID')
app_id = request.headers.get('X-IBS-APP-ID')
response_data = \
{
"vova": "cooper",
"api_id": api_id,
"app_id": app_id,
}
response_json = json.dumps(response_data)
return Response(response=response_json,
status=200,
mimetype="application/json",
headers={"api_id": api_id, "app_id": app_id})
########################################################################################################################
#WEBSOCKET
########################################################################################################################
@app.route("/ws")
def web_socket():
return render_template('websocket_test.html')
@ws.route('/websocket')
def web_socket_echo(ws):
try:
logger.warning("/websocket")
while True:
msg = ws.receive()
if msg is not None:
ws.send(msg)
else:
return
except Exception, e:
logger.exception(e)
response = Response(response=None, status=200)
return response
@ws.route('/echo')
def echo(ws):
while True:
message = ws.receive()
ws.send(message)
########################################################################################################################
if __name__ == "__main__":
logger.info("Starting application!")
app.run(host='0.0.0.0')
#, gevent=1000