im folgende eine (rel. einfache) Nutzer-Authentifizierung via Bottle via Cookie:
Code: Alles auswählen
import bottle
from hashlib import md5
userdict = {
'OttoNormal':'7\xfc~\xde\xe2Z\x17tt\xea\xeb\xe7\xf7\xb0\x97\x85',\
'RainerTitan':'>\xb3\x8f\xa8\x07\x9f\x88s$\xe8\xee\x1d\x92\xb3\xda\x12',\
'VolkerRacho':'1\xd4T\x1b\x8e\x92j$\xf0\xc9\xb85\xb6\x8c\xfd\xf3'}
#userdict = {'OttoNormal':'flat','RainerTitan':'steel','VolkerRacho':'fast'}
@bottle.route('/start')
def start():
return '<a href="/public">Public Page</a> or go to <a href="/login">Log-in</a>'
@bottle.route('/public')
def public():
return 'This is a public page'
@bottle.route('/login')
def login():
return '''<html>
<form action="/checklogin" method="POST">
Name: <input type="text" name="user" length="20">
Password: <input type="password" name="passw" length="20">
<input type="submit" value="send">
</html>'''
@bottle.route('/checklogin', method='POST')
def checklogin():
if not bottle.request.forms.get('user'):
return 'Go to log in page first!'
user = bottle.request.forms.get('user')
passw = bottle.request.forms.get('passw')
if user not in userdict:
return bottle.template('Sorry, the user {{user}} is not registered here!', user=user)
elif md5(passw).digest() != userdict[user]:
return 'Wrong password!'
else:
bottle.response.set_cookie('user',user,secret='arealsecret')
return bottle.template('You are logged in now, user {{user}}', user=user)
@bottle.route('/secretpage')
def secretpage():
try:
user = bottle.request.get_cookie('user',secret='arealsecret')
except:
user = None
if not user:
return 'Log in first!'
else:
return 'This is a secret page :-)'
bottle.debug(True)
bottle.run(reloader=True)
Gruß, noisefloor