Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to Python3.4 #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions app2
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python

from bottle import route, view, post
import bottle, bottlesession


####################
#session_manager = bottlesession.PickleSession()
session_manager = bottlesession.CookieSession()
valid_user = bottlesession.authenticator(session_manager)

@route('/')
@route('/:name')
@valid_user()
def hello(name = 'world'):
return '<h1>Hello %s!</h1>' % name.title()

@route('/auth/login')
@post('/auth/login')
@view('html/login.html')
def login():
passwds = { 'guest' : 'guest' }

username = bottle.request.forms.get('username')
password = bottle.request.forms.get('password')

if not username or not password:
return { 'error' : 'Please specify username and password' }

session = session_manager.get_session()
session['valid'] = False

if password and passwds.get(username) == password:
session['valid'] = True
session['name'] = username

session_manager.save(session)
if not session['valid']:
return { 'error' : 'Username or password is invalid' }

bottle.redirect(bottle.request.get_cookie('validuserloginredirect', '/'))

@route('/auth/logout')
def logout():
session = session_manager.get_session()
session['valid'] = False
session_manager.save(session)


##################
app = bottle.app()
if __name__ == '__main__':
bottle.debug(True)
bottle.run(app = app)
8 changes: 5 additions & 3 deletions bottlesession.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,18 @@ def load(self, sessionid):
filename = os.path.join(self.session_dir, 'session-%s' % sessionid)
if not os.path.exists(filename):
return None
with open(filename, 'r') as fp:
with open(filename, 'rb') as fp:
session = pickle.load(fp)
return session

def save(self, data):
sessionid = data['sessionid']
fileName = os.path.join(self.session_dir, 'session-%s' % sessionid)
tmpName = fileName + '.' + str(uuid.uuid4())
with open(tmpName, 'w') as fp:
with open(tmpName, 'wb') as fp:
self.session = pickle.dump(data, fp)
if os.path.isfile(fileName):
os.remove(fileName)
os.rename(tmpName, fileName)


Expand Down Expand Up @@ -169,7 +171,7 @@ def __init__(
else:
# save off a secret to a tmp file
secret = ''.join([
random.choice(string.letters)
random.choice(string.ascii_letters)
for x in range(32)])

old_umask = os.umask(int('077', 8))
Expand Down
9 changes: 9 additions & 0 deletions html/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%if error:
<fieldset><legend>Notice</legend>{{error}}</fieldset>
%end

<form method="POST" id="form" action="/auth/login">
Login name: <input type="text" name="username" /><br/>
Password: <input type="password" name="password" /><br/>
<input type="submit" value="Login" name="submit" />
</form>