Table of contents
1.
Introduction
2.
Cookies in web2py
3.
Application Initialization in web2py
4.
Frequently Asked Questions
4.1.
What Exactly is web2py?
4.2.
What are Cookies in web2py?
4.3.
Which command is used for storing Cookies?
4.4.
Which command is used for setting Cookies?
4.5.
What is Application Initialization in web2py?
5.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Cookies and Application Initialization in web2py

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Web2Py is one of the most popular Python web frameworks. Web2Py has a Python interpreter in addition to being developed in Python. Furthermore, the full-stack web framework functions seamlessly on major operating systems and web servers without the need for pre-installation.

Web2py

 

Web2py is available in both source code and binary form. And one of the important things to keep in mind while doing web development is Managing Cookies and Application Initialization in web2py, which we will be explaining to you all via this blog. Cookies and Application Initialization in web2py is done with Python Modules. A module is a Python definition and statement file. Within a module, the name (as a string) of the module is available as the value of the global variable __name__. So, if you want to learn more about Cookies and Application Initialization in web2py, keep reading.

Cookies in web2py

Cookies in web2py

For cookie processing, web2py makes use of the Python cookies modules. Cookies from the browser are being request.cookies and Cookies supplied by the server are in response.cookies. 

A cookie can be set as follows:

  • You may then access your cookie as follows:
response.cookies['cookie'] = 'somevalue'
You can also try this code with Online Python Compiler
Run Code

 

  • To create a cookie that will expire in one day, use:
response.cookies['cookie']['expires'] = 24 * 3600
You can also try this code with Online Python Compiler
Run Code

 

  • If you want the cookie to be available from other functions/controllers, you must specify its path:
response.cookies['cookie']['path'] = '/'
You can also try this code with Online Python Compiler
Run Code

 

The second command instructs the browser to hold the cookie for a period of 24 hours. The final command instructs the browser to deliver the cookie back to any application at the current domain (URL path). Please keep in mind that if you do not specify a path for the cookie, the browser will presume the path of the URL that was requested. Therefore the cookie will only be delivered to the server when that same URL path is requested.

The cookie may be made secure by including:

response.cookies['cookie']['secure'] = True
You can also try this code with Online Python Compiler
Run Code

 

This instructs the browser to only transmit the cookie back over HTTPS and not HTTP.

The cookie may be obtained by using: 

if request.cookies.has_key('cookie'):
    value = request.cookies['cookie'].value
You can also try this code with Online Python Compiler
Run Code

 

Unless sessions are deactivated, web2py sets and utilizes the following cookie to manage sessions:

response.cookies[response.session_id_name] = response.session_id
response.cookies[response.session_id_name]['path'] = "/"
You can also try this code with Online Python Compiler
Run Code

 

Please keep in mind that if a single application has numerous subdomains and you wish to share the session across those subdomains, you must explicitly define the domain of the session cookie as follows:

if not request.env.remote_addr in ['127.0.0.1', 'localhost']:
   response.cookies[response.session_id_name]['domain'] = ".yourdomain.com"
You can also try this code with Online Python Compiler
Run Code

 

The preceding can be useful if, for example, You want the user to be able to log in across subdomains.

Application Initialization in web2py

Application Initialization in web2py

When you deploy web2py, you should specify a default application, which is the application that runs when the URL contains an empty path, like in:

http://137.0.0.1:8200

 

When presented with an empty path, web2py looks for an application named init by default. If there is no init application, it looks for a welcome application.

The default application's name may be changed from init to another one by changing the default application option in routes.py:

default_application = "app"
You can also try this code with Online Python Compiler
Run Code

 

Here are four methods for changing the default application:

  • "init" your default application.
  • In routes.py, replace the default application with the name of your application.
  • Create a symbolic link from "applications/init" to the folder containing your application.
  • Use URL rewriting.

Frequently Asked Questions

What Exactly is web2py?

Web2py is a simple framework that requires no installation or configuration. This framework is portable and can run on a USB disc. It, like many other Python frameworks, is built on the MVC framework.

What are Cookies in web2py?

Web2py makes use of SimpleCookie objects from Python. Text can be kept in cookies, and cookies are stored on the client's end.

Which command is used for storing Cookies?

Request.cookies is the command used for storing Cookies.

Which command is used for setting Cookies?

Response.cookies is the command used for setting Cookies.

What is Application Initialization in web2py?

When you deploy web2py, you should specify a default application, which is the application that runs when the URL contains an empty path.

Conclusion 

In this article, we learned about Cookies and Application Initialization in web2py, which includes basic Commands/codes of Cookies and Application Initialization in web2py.

After reading about Cookies and Application Initialization in web2py, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. To learn, see Django IntroductionDjango Formsets, and Django Model Form.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! 

Do upvote our blogs if you find them helpful and engaging!

Live masterclass