Bottle: accessing the same session on different Canister devices

Asked

Viewed 42 times

0

Hello, I am developing a project to study character and I am with a great inconvenience.

I’m using:

  • astroid==2.0.4
  • Bottle==0.12.13
  • canister==1.5.1
  • Colorama==0.4.0
  • cymysql==0.9.12
  • Cython==0.29
  • isort==4.3.4
  • Lazy-Object-proxy==1.3.1
  • Mccabe==0.6.1
  • Pyjwt==1.6.4
  • pylint==2.1.1
  • Six==1.11.0
  • wrapt==1.10.11

As you can see I use canister to create and maintain my sessions, the problem I face is the following, when entering login and password, perform existence confirmation and data confirmation in the database and start a session, if I access mine using the (ip of my computer):8080 it does not take me to the home screen, does not take me to the login screen, takes me to my Dashboard, as if I had already logged on to that device, I thought maybe it could be problem with my network or something, this way I stayed in the pythonanywhere and continued with same problem.

from bottle import Bottle, TEMPLATE_PATH
from Data.SQL import MySQL
import os
import canister
from canister import session

# Diretorio base.
base_path = os.getcwd().replace('\\', '/')
# Instancia da aplicacao.
app = Bottle()
# Path de configuracao.
app.config.load_config('{}/config/lobster.config'.format(base_path))
# Instalacao do plugin de sessoes.
app.install(canister.Canister())

# Instancia do banco de dados.
banco_mysql = MySQL()

from app.Controllers import *

When the query is performed and the data is validated the following methods are executed creating both the sessions and the cookies

def definir_cookie_login(self, usuario_id, nome, email, na, unidade):
    response.set_cookie('usuario_id', str(usuario_id))
    response.set_cookie('nome_usuario', str(nome))
    response.set_cookie('email', str(email))
    response.set_cookie('na', str(na))
    response.set_cookie('unidade', str(unidade))

def iniciar_sessao(self, usuario_id, nome, email, na, unidade):
    session.data['usuario_id'] = str(usuario_id)
    session.data['nome_usuario'] = str(nome)
    session.data['email'] = str(email)
    session.data['na'] = str(na)
    session.data['unidade'] = str(unidade)

Anybody got any ideas? how can I make it so that every attempt to access a different device or even a different browser is created a new session instead of accessing the one already used.

1 answer

0


I realized that when I created the session.data he created only one session.data for all sessions, as I needed a form to difficult the sessions I decided to create the keys of the session.data using something that differs each session and still allowed the session to continue, so I used the session.sid concatenated with the keys I was creating, I don’t know if this is the most pythonica (probably not) or the most correct (most probably not), but it was the way I was able to solve my problem and now it’s working, I deployed it by pythonanywhere.com. I tested on multiple devices and so far had no problem with performance or duplicate sessions, at first I thought of using the client’s ip, however request.environ.get('REMOTE_ADDR') provides me the client’s local ip or it would be possible to have two people accessing using the 192.168.0.x or 192.168.15.x etc....

The method that starts my session is as follows:

def iniciar_sessao(self, usuario_id, nome, email, na, unidade):
    session.user = request.environ.get('REMOTE_ADDR')
    session.data[session.sid+'usuario_id'] = str(usuario_id)
    session.data[session.sid+'nome_usuario'] = str(nome)
    session.data[session.sid+'email'] = str(email)
    session.data[session.sid+'na'] = str(na)
    session.data[session.sid+'unidade'] = str(unidade)

And to end the sessions:

def encerrar_sessao(self):
    del session.data[session.sid+'usuario_id']
    del session.data[session.sid+'nome_usuario']
    del session.data[session.sid+'email']
    del session.data[session.sid+'na']
    del session.data[session.sid+'unidade']
    session.user = None

If anyone knows in any more practical way (or more pythonica) can share with me, I would like to improve my codes.

In case the form is correct, I hope you help someone else.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.