Skip to main content

how to use redis as flask session storage

 
Im this tutorial we simply configured redis locally through docker. We can easily configure it with the command below.  

$ docker pull redis:latest $ docker run -d -p 6379:6379 --name session_storage redis:latest

We can check the status of the redis container by entering the command below.

$ docker ps CONTAINER ID  
 

Below is a simple web application that uses Python flask to store and retrieve information in sessions. 

 import redis
from flask import *
from flask_session import Session

app = Flask(__name__)

app.secret_key = 'Skills39Test'

app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_PERMANENT'] = False
app.config['SESSION_USE_SIGNER'] = True
app.config['SESSION_REDIS'] = redis.from_url('redis://localhost:6379')

server_session = Session(app)

@app.route('/info', methods=['GET','POST'])
def set():
    if request.method == 'GET':
        username = session['username']
        name = session['name']
        message = session['message']
        return jsonify({'username':username,'name':name,'message':message})

    else:
        session['username'] = request.form.get('username')
        session['name'] = request.form.get('name')
        session['message'] = request.form.get('message')
        return jsonify({'upload':'success'})


app.run(host='0.0.0.0', port=8080)

Create the below file for dependencies:
# requirements.txt
flask
flask_session
redis




To check, run the below code using python to check the endpoint response

import requests
res = requests.post("http://localhost:8080/customer", data={'username':'sysops1', 'name':'Bagus Santosa', 'message': 'Hallo World'})
print(res.status_code)

requests.get("http://localhost:8080/info", cookies={'session':session})
print(res.json())

To check inside the Redis. We can access the redis container using the command below. 

$ docker exec -it session_storage redis- 127.0.0.1:6379> keys * 1) "session:session_id"

We can view the information stored in the session by entering the command below. 

127.0.0.1:6379> get session:session_id

Thank you.

Comments

Popular posts from this blog

Troubleshooting WAF, CDN, and Load balancer

 Troubleshooting issues involving a Web Application Firewall (WAF), Content Delivery Network (CDN), and Load Balancer (LB) can be complex. Here is a step-by-step guide, including testing methods and relevant commands to isolate problems at each level. ### Step 1: Check Basic Connectivity 1. **Ping the Website**:    ```sh    ping example.com    ``` 2. **Check DNS Resolution**:    ```sh    nslookup example.com    ``` ### Step 2: Verify Load Balancer 1. **Direct Access to Load Balancer**:    Modify `/etc/hosts` to bypass CDN and WAF, pointing directly to the load balancer’s IP.    ```sh    sudo nano /etc/hosts    ```    Add the line:    ```    LB_IP example.com    ```     2. **Test HTTP Response**:    ```sh    curl -I http://example.com    ```    Check headers and status code to verify the load bal...

Soal dan pembahasan LKSN ITNSA

Berikut adalah koleksi soal-soal ITNSA dan beberapa website yang memberikan pembahasan pada event perlombaaan LKS SMK Nasional bidang lomba IT Network Systems Administration. 2014 Palembang Di tahun ini ada 1 soal packet tracer. Di website dibawah ini diberikan juga cara pembahasannya. https://agussas.wordpress.com/2015/04/02/review-soal-lks-nasional-it-network-23-packet-tracer-challenge/ Semua soal:  https://www.dropbox.com/sh/l90zyke2ib5msgv/AAA3kPOFo-zEn4wPOW4a3iMwa?dl=0 dan https://drive.google.com/file/d/18lDhtMjAnPAhkfOJ6uFHsC5j6ycg3K4I/view Pembahasan juga dalam bentuk video di youtube:  https://www.youtube.com/watch?v=8QML594nQBU 2015 Banten Pembahasan:   https://www.youtube.com/watch?v=quDbpC2xSfQ Soal:  https://drive.google.com/file/d/1B09IYfdoGENBL3txSQodpptG1zdQxBWI/view 2016 Malang Soal:   https://drive.google.com/file/d/13-2bRtb5IXO9vxAhLfhghZbDXeUzD0FI/view Pembahasan:   https://www.youtube.com/watch?v=zmUSUZguH24 20...

LKS SMK Cloud Computing

 Lomba Kompetensi Siswa SMK merupakan salah satu kegiatan rutin yang dilakukan tiap tahun, mulai dari tingkat kota/kabupaten sampai Internasional (Asean, WorldSkills Competiton). Untuk kali ini akan membahas salah satu bidang yang belum terlalu lama yaitu cloud computing, tentu dunia cloud computing sudah lama beredar, dan AWS salah satu leadernya, untuk kegiatan kompetisi lomba cloud computing ini mulai dirintis sejak 2017 di worldskills abu dhabi, saat itu masih dalam proses demo, hingga pada tahun 2019 menjadi salah satu bidang resmi di worlskills kazan. AWS sendiri sebetulnya punya kegiatan kompetisi sendiri juga seperti Hackathon dan AWS Gaming Day. Pada blogpost ini akan berusaha mencari informasi tentang cloud computing kompetisi khususnya LKS SMK seperti kisi-kisi. Berikut adalah kurasi link-link yang bermanfaat untuk belajar,  terima kasih juga pada penulis di website-website berikut:  Kisi-kisi LKS di salah satu Provinsi https://github.com/stmj-dev/Soal-Dan-Kisi...