Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 470 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What Unix tool to quickly add/remove some text to a Python script?

#1
I'm developing an application using <a href="http://flask.pocoo.org/">Flask</a>.

I want a quick, automated way to add and remove `debug=True` to the main function call:

Development:

app.run(debug=True)

Production:

app.run()

For security reasons, as I might expose private/sensitive information about the app if I leave debug mode on "in the wild".

I was thinking of using sed or awk to automate this in a git hook (production version is kept in a bare remote repo that I push to), or including it in a shell script I am going to write to fire up uwsgi and some other "maintenance"-ey tasks that allow the app to be served up properly.

What do you think?
Reply

#2
I'd use `sed`:

sed 's/debug=True//'

portable, scriptable, ubiquitous.
Reply

#3
You should set up some environment variable on server. Your script can detect presense of this variable and disable debugging.
Reply

#4
You can also use a NOCOMMIT hook (from [gitty](

[To see links please register here]

)):

Set this as a pre-commit hook

if git diff --cached | grep NOCOMMIT > /dev/null; then
echo "You tried to commit a line containing NOCOMMIT"
exit 1
fi
exit 0

This will prevent the commit if it contains `NOCOMMIT`.

You can of course directly replace `NOCOMMIT` by `Debug=True` in the hook.
Reply

#5
You probably [should not be using `app.run` in production][1] (and you *definitely* don't need it [if you are using uwsgi][2]). Instead, use one of the several deployment options discussed in the [deployment][3] section of Flask's *excellent* documentation. ([`app.run`][4] simply calls [`werkzeug.serving.run_simple`][5] which executes Python's included [`wsgiref`][6] server.)

That being said, the correct way to do this is not with a post-deploy edit to your source code but with a server-specific config file that changes your settings as [@brandizzi pointed out in his answer][7].

You can do this in several different ways (Flask has [documentation on this too][8] - see Armin's suggestions on [configuring from files][9] and [handling the development-production switch][10]):

1. Include both your development and your server's configs in your repository. Use an environmental variable to switch between them:

# your_app.config.develop
DEBUG = True

# your_app.config.production
DEBUG = False

# your_app.app
from flask import Flask
from os import environ

mode = environ.get("YOURAPP_MODE")
mode = "production" if mode is None else "develop"

config = __import__("your_app.config." + mode)

app = Flask("your_app")
app.config.from_object(config)

1. Store your production configuration in a separate repository along with any other server-specific configurations you may need. Load the config if an environmental variable is set.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

[5]:

[To see links please register here]

[6]:

[To see links please register here]

[7]:

[To see links please register here]

[8]:

[To see links please register here]

[9]:

[To see links please register here]

[10]:

[To see links please register here]

Reply

#6
That is not the way to go! My recommendation is to create some configuration Python module (let us say, `config.py`) with some content such as:


DEBUG = True


Now, in our current code, write this:

import config
app.run(debug=config.DEBUG)

Now, when you run in production, just change `DEBUG` from `True` to `False`. Or you can leave this file unversioned, so the copy of development is different of the copy of production. This is not uncommon since, for example, one does not use the same database connection params both in development and production.

Even if you want to update it automatically, just call sed on the config file with the `-i` flag. It is way more secure to update just this one file:

$ sed -i.bkp 's/^ *DEBUG *=.*$/DEBUG = False/' config.py
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through