Use beaker to enable Facebook's bottle framework to support session functionality

Source: Internet
Author: User
Tags extend

use Beaker to enable Facebook's bottle framework to support session functionality

This article mainly introduces the use of beaker to allow Facebook's bottle framework to support the session function, the session in the python, such as the Django framework built-in but in the bottle is not integrated, need friends can refer to the

Bottle is a small web framework, very small only one file, but the function is very powerful, learning is also simple, simple and small also have a lot of deficiencies, some functional support is not very perfect, such as session. But it has its own benefits, and we can extend it by ourselves or by using other modules, Unlike Django, it's very powerful, but when you want to extend it, you don't have the strength to do it. We can make it more powerful and perfect by bottle ourselves with very simple and powerful hands.

Bottle Compact Support Cookie but does not support session. For security reasons we sometimes want to use the session. We can use middleware beaker to extend bottle, Enable our bottle application to support session. Don't say much nonsense. First of all, Beaker is not a built-in module, we first install it. Of course you can manually install the package online, we use the simplest:

?

1 Easy_install Beaker

No easy_install This command? Google Bar, after installed or not, such as outdated win words check the environment variable, add the scripts directory under the Python installation directory to the environment variable.

How do we use it when installed, and the following section is used with a surface:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20-21 #!/usr/bin/env python from bottle import route, Default_app, run, request from Beaker.middleware import Sessionmiddleware session_opts = {' Session.type ': ' File ', ' session.cookei_expires ': ', ' session.data_dir ': './sessions ', ' Sessioni.auto ': True} @route ('/test ') def test (): s = request.environ.get (' beaker.session ') s[' test ' = s.get (' Test ', 0) + 1 S.save () return ' Test conter:%d '% s[' test '] app = Default_app () app = Sessionmiddleware (app, session_opts) run (AP P=app)

Running this code prompts you to:

?

1 2 3 Bottle Server starting up (using Wsgirefserver ()) ... Listening on Http://127.0.0.1:8080/Hit ctrl-c to quit.

Now open browser Access http://127.0.0.1:8080/test

The constant refresh will find that the value is constantly increasing. It means our session is working properly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.