-
Notifications
You must be signed in to change notification settings - Fork 1
/
flaskrun.py
executable file
·53 lines (44 loc) · 1.67 KB
/
flaskrun.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
#!/usr/bin/env python3
# Based on: http://flask.pocoo.org/snippets/133/
import optparse
def flaskrun(app,
default_host="0.0.0.0",
default_port="5000",
**kwargs):
"""
Takes a flask.Flask instance and runs it. Parses
command-line flags to configure the app.
"""
# Set up the command-line options
parser = optparse.OptionParser()
parser.add_option("-H", "--host",
help="Hostname of the Flask app " + \
"[default %s]" % default_host,
default=default_host)
parser.add_option("-P", "--port",
help="Port for the Flask app " + \
"[default %s]" % default_port,
default=default_port)
# Two options useful for debugging purposes, but
# a bit dangerous so not exposed in the help message.
parser.add_option("-d", "--debug",
action="store_true", dest="debug",
help=optparse.SUPPRESS_HELP)
parser.add_option("-p", "--profile",
action="store_true", dest="profile",
help=optparse.SUPPRESS_HELP)
options, _ = parser.parse_args()
# If the user selects the profiling option, then we need
# to do a little extra setup
if options.profile:
from werkzeug.contrib.profiler import ProfilerMiddleware
app.config['PROFILE'] = True
app.wsgi_app = ProfilerMiddleware(app.wsgi_app,
restrictions=[30])
options.debug = True
app.run(
debug=options.debug,
host=options.host,
port=int(options.port),
**kwargs
)