How can I made a flask app availible for one client at the same time

485 views Asked by At

I want to make a webapp which can only be edited by one client at the same time. I use flask and flask-socketio. After the authorized client leaves (disconnect event in socketio) the next should be able to edit things. Right now I have multiple calls to websockets and http gets not just one websocket. Because I have simple html forms as well I have to block incoming request too.

I could get the IP from a request object in a socketio event.

connected = None

@socketio.on('disconnect', namespace='/notifications')
def disconnect():
    global connected
    connected = None
    logger.info('Client disconnected')

@socketio.on('connect', namespace='/notifications')
def notifications():
    global connected
    if not request.headers.getlist("X-Forwarded-For"):
       ip = request.remote_addr
    else:
       ip = request.headers.getlist("X-Forwarded-For")[0]
    if connected is None:
        connected = ip, request.environ["REMOTE_PORT"]
        logger.info("Client connected. %s:%s", connected[0],connected[1])
        # do stuff
    else:
        logger.info("Blocked %s:%s", ip, request.environ["REMOTE_PORT"])

It looks like a bad approach to me...

0

There are 0 answers