def parse_and_run(args=None):
sslBaseDir = path.join(BASE_DIR, 'ssl')
p = ArgumentParser()
p.add_argument('--bind', '-b', action='store', help='the address to bind to', default='127.0.0.1')
p.add_argument('--port', '-p', action='store', type=int, help='the port to listen on', default=8080)
p.add_argument('--debug', '-d', action='store_true', help='enable debugging (use with caution)', default=False)
p.add_argument('--ssl', '-s', action='store_true', help='enable ssl', default=False)
args = p.parse_args(args)
if args.ssl:
ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ctx.load_cert_chain(path.join(sslBaseDir, 'server.crt'), path.join(sslBaseDir, 'server.key'))
app.config['SESSION_TYPE'] = 'filesystem'
app.run(host=args.bind, port=args.port, debug=args.debug, ssl_context=ctx)
else:
app.run(host=args.bind, port=args.port, debug=args.debug)
python类run()的实例源码
def test(coverage=False):
import unittest2
import os
import coverage as _coverage
cov = None
if coverage:
cov = _coverage.coverage(branch=True, include='./*')
cov.start()
tests = unittest2.TestLoader().discover('tests')
unittest2.TextTestRunner(verbosity=2).run(tests)
if cov:
cov.stop()
cov.save()
print('Coverage Summary:')
cov.report()
basedir = os.path.abspath(os.path.dirname(__file__))
covdir = os.path.join(basedir, 'tmp/coverage')
cov.html_report(directory=covdir)
print('HTML version: file://%s/index.html' % covdir)
cov.erase()
# TODO: Implement options
def stuff_before_request():
g.request_start_time = time()
g.hybrid = False
if 'hybrid' in request.args.keys():
g.hybrid = True
logger.info(u"GOT HYBRID PARAM so will run with hybrid.")
# don't redirect http api in some cases
if request.url.startswith("http://api."):
return
if "staging" in request.url or "localhost" in request.url:
return
# redirect everything else to https.
new_url = None
try:
if request.headers["X-Forwarded-Proto"] == "https":
pass
elif "http://" in request.url:
new_url = request.url.replace("http://", "https://")
except KeyError:
# logger.info(u"There's no X-Forwarded-Proto header; assuming localhost, serving http.")
pass
# redirect to naked domain from www
if request.url.startswith("https://www.oadoi.org"):
new_url = request.url.replace(
"https://www.oadoi.org",
"https://oadoi.org"
)
logger.info(u"URL starts with www; redirecting to " + new_url)
if new_url:
return redirect(new_url, 301) # permanent
# convenience function because we do this in multiple places
def compile():
"compile translations"
os.system(pybabel + ' compile -d app/translations')
#run with ssl certs
def ssl():
from OpenSSL import SSL
ctx = SSL.Context(SSL.SSLv23_METHOD)
ctx.use_privatekey_file('ssl.key')
ctx.use_certificate_file('ssl.cert')
app.run(ssl_context=ctx)
def run_app():
from subprocess import Popen
import sys
os.chdir(os.path.abspath(os.path.dirname(__file__)))
path = os.path.join(os.path.abspath(os.path.dirname(__file__)), "manage.py")
args = [sys.executable, path, "db"]
if not os.path.exists(os.path.join(config.DATA_DIR, "plexivity.db")):
from app import db
db.create_all()
args.append("stamp")
args.append("head")
else:
args.append("upgrade")
Popen(args)
helper.startScheduler()
if config.USE_SSL:
helper.generateSSLCert()
try:
from OpenSSL import SSL
context = SSL.Context(SSL.SSLv23_METHOD)
context.use_privatekey_file(os.path.join(config.DATA_DIR, "plexivity.key"))
context.use_certificate_file(os.path.join(config.DATA_DIR, "plexivity.crt"))
app.run(host="0.0.0.0", port=config.PORT, debug=False, ssl_context=context)
except:
logger.error("plexivity should use SSL but OpenSSL was not found, starting without SSL")
app.run(host="0.0.0.0", port=config.PORT, debug=False)
else:
app.run(host="0.0.0.0", port=config.PORT, debug=False)
def run(self):
# Define your tasks here
# Anything written in python is permitted
# For example you can clean up your server logs every hour
run_app()
def create():
print("Initialising")
init()
application.run(host='127.0.0.1', port=5000)
def main():
initialize_app(app)
app.jinja_env.auto_reload = True
app.run(debug=True)
def main():
# Set up logging
logging.basicConfig(format='%(asctime)s - %(levelname)s %(funcName)s() - %(message)s',
filename=config.log_file_name,
level=config.log_level)
logging.info('-- Starting the Software Assessment Framework --')
app.config['WTF_CSRF_ENABLED'] = False
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.run(host=config.host, port=config.port, debug=True)
def runserver():
from eventit.eventit import app
app.run(
host="0.0.0.0",
port=8080,
debug=True
)
# TODO: Create admin user
def main(argv):
from app import app
app.debug = "debug" in argv or IS_DEBUG
app.run(port=5006)
def runserver(host, port, debug):
"""Run server"""
app.run(host=host, port=port, debug=debug)