Secret_key key generated in Python's flask framework

Source: Internet
Author: User
Intro

If you encounter a must provide Secret_key to use CSRF error alert, the reason is not set Secret_key, add the code

app.config[' secret_key ']= ' xxx '

Secret_key best not to write in the code.
It is best to set up a config.py file from which to read the content
config.py

csrf_enabled = Truesecret_key = ' you-will-never-guess '

app.py

App.config.from_object (' config ')

This will prevent the CSRF.
Well, then let's talk about this secret_key:

Flask a simple way to generate Secret_key (key)
Secret_key is one of the more important configuration values in flask. This paper introduces a relatively simple method of generating secret_key.

Sessions, cookies, and some third-party extensions will all use the Secret_key value, which is a more important configuration value and should be set to a hard-to-guess value, preferably at random.

The stochastic problem is that it is difficult to judge what is true random. A key should be random enough. Your operating system can generate beautiful random values based on a random password generator, which can be used to make a key:

>>> import os>>> os.urandom ' \xca\x0c\x86\x04\x98@\x02b\x1b7\x8c\x88]\x1b\xd7 ' +\xe6px@\xc3#\ \'

Copy and paste this value into your code, and you're done with the key.

Setup method:

App = Flask (__name__) app.config[' secret_key '] = ' \xca\x0c\x86\x04\x98@\x02b\x1b7\x8c\x88]\x1b\xd7 ' +\xe6px@\xc3#\\ ' # Orapp.secret_key = ' \xca\x0c\x86\x04\x98@\x02b\x1b7\x8c\x88]\x1b\xd7 ' +\xe6px@\xc3#\\ ' # orapp.config.update ( secret_key= ' \xca\x0c\x86\x04\x98@\x02b\x1b7\x8c\x88]\x1b\xd7 ' +\xe6px@\xc3#\\ ')


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.