-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·61 lines (48 loc) · 1.62 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
#!/usr/bin/env python
import os
from flask import Flask
# There are two contexts in Flask: the application context and the request context.
from flask import request
from flask import make_response
from flask import redirect
from flask import abort
from flask import render_template
from flask.ext.bootstrap import Bootstrap
# Flask uses the __name__ argument to determine the root path of the application so that
# it later can find resource files relative to the location of the application.
app = Flask(__name__)
boostrap = Bootstrap(app)
@app.route('/')
def index():
#return '<h1>Hello World!</h1>'
return render_template('index.html')
@app.route('/user/<name>')
def user(name):
#return '<h1>Hello, %s!</h1>' % name
return render_template('user.html', name=name)
@app.route('/browser')
def get_browser():
user_agent = request.headers.get('User-Agent')
return '<h1>Your browser is %s</h1>' % user_agent
@app.route('/set_cookie')
def set_cookie():
response = make_response('<h1>This document carries a cookie!</h1>')
response.set_cookie('answer', '42')
return response
@app.route('/redirect')
def test_redirect():
return redirect('http://www.bing.com')
@app.route('/abort')
def test_abort():
abort(404)
return '<h1>You should not see this.</h1>'
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_server_error(e):
return render_template('500.html'), 500
if __name__ == '__main__':
host = os.environ.get('HOST', '0.0.0.0')
port = os.environ.get('PORT', '5000')
app.run(host=host, port=int(port), debug=True)