Bottle: accessing the same session on different canister devices

Hello, I am developing a project to study character and I am having 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
  • wrap = = 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, make a confirmation and receipt of data in the database, and start a session, if I look at my using the ip address of my computer:8080 it doesn't take me to the home screen, it is not, it takes me to the log in screen, it takes me to my dashboard, and I have already logged in on that device, I thought it might be a problem with my network or something like that, this way, hosted in pythonanywhere and I kept with the 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 valid the following methods are executed by creating both sessions and 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)

Anyone have any ideas? how can I make every access attempt on a different device or even on a different browser create a new session instead of accessing the one that is already being used.

Author: Afonso Medeiros, 2018-12-10

1 answers

I Noticed that when I used the session.data - he created only one of session.data for all of the sessions, as I needed to get a form to differentiate the sessions, I decided to create the keys in the session.data - using something that is difference of each session, and even then it would allow it to persist in the session, then use the session.sid the concatenated with the key that I was creating, I don't know if this is the pythonica (probably not) or to the right (probably not), but it was the way I was able to solve my problem and now it's working, I did deploy by pythonanywhere.com . I tested on several devices and so far I had no problem with performance or duplicate sessions, at first I thought of using the client's ip, but request.environ.get('REMOTE_ADDR') provides me with the client's local ip ie it would be possible to have two people accessing using ip 192.168.0.x or 192.168.15.X etc...

The method that initializes my session looked like this:

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 close 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 some way more practical (or more Pythonic) can share with me, I would like to improve my codes.

If the shape is correct, I hope it helps someone else.

 0
Author: Afonso Medeiros, 2018-12-12 13:48:41