Home

Tags

HTTP Авторизация на bottle

2010-04-01 web bottle

Пример простой HTTP авторизации

# coding: utf8

from bottle import run, route, abort, response, request

USER = 'user'
PASW = 'pasw'

def login():
    if ('auto' in request.COOKIES) and (request.COOKIES['auto'] == '1'): return
    try:
        h = request.header['Authorization']
        if h[:6] == 'Basic ':
            login,pasw = h[6:].decode('base64').split(':')
            if (login == USER) and (pasw == PASW):
                response.set_cookie('auto', '1')
                return
    except:
        pass
    response.header['WWW-Authenticate'] = 'Basic realm="Control Panel"'
    abort(401)

@route('/logout')
def logout():
    response.set_cookie('auto', '')
    return 'logout ok'

@route('/')
def main():
    login()
    return 'Hello world<br/><a href="/logout">logout</a>'

run(port=8080)



Пример авторизации с уникальными куками (более-менее надежная авторизация) Простая авторизация в web

comments powered by Disqus