Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8827593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:24:33+00:00 2026-06-14T07:24:33+00:00

I’m writing a bottle application with beaker session middleware. My code is has this:

  • 0

I’m writing a bottle application with beaker session middleware.

My code is has this:

@bottle.route('/')
def slash():

  try:
    beaker_session = request.environ['beaker.session']
  except:
    #redirect('/login')
    abort(401, "Failed beaker_session in slash")

  try:
    name = beaker_session['name']
  except:
    redirect('/login')

for each route request except /login. I know there is a bottle hook system to do things before requests, but I’m not sure how best to use it to check if someone is logged in or not.

I’m fairly new to python webapps using bottle. Not to many people are using it with beaker session middleware so I don’t have a lot of examples to go by.

Thanks for any help or pointers!

PS. The entire code for this is in this repo: https://github.com/curtisgithub/labinski/blob/master/labinski.py

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:24:34+00:00Added an answer on June 14, 2026 at 7:24 am

    I know there is a bottle hook system to do things before requests, but I’m not sure how best to use it to check if someone is logged in or not.

    You can use the before_request hook to run code prior to each request, but checking authentication here only makes sense if you expect all access to be authenticated. You could do something like this:

    @bottle.hook('before_request')
    def setup_request():
        try:
            beaker_session = request.environ['beaker.session']
        except:
            #redirect('/login')
            abort(401, "Failed beaker_session in slash")
    
        try:
            name = beaker_session['name']
        except:
            redirect('/login')
    

    …but without some extra code this is going to result in a redirect loop when someone actually requests /login. So you can add this to the hook, maybe:

    if request.urlparts.path == '/login':
        continue
    

    Another solution is to implement similar functionality using a Python decorator, which lets you control access on a method-by-method basis. For example, you could say something like:

    @route('/')
    @authenticated
    def index():
        return 'This is /.'
    
    @route('/login')
    def login():
        return 'This is login.'
    

    And then your authenticated decorator would look very much like the hook:

    def authenticated(func):
        def wrapped(*args, **kwargs):
            try:
                beaker_session = request.environ['beaker.session']
            except:
                abort(401, "Failed beaker_session in slash")
    
            try:
                name = beaker_session['name']
                return func(*args, **kwargs)
            except:
                redirect('/login')
    
        return wrapped
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.