Introduction
We all know What Python is and What we can do with Python. Python is frequently used for the development of big data applications and the analysis of massive amounts of data. As a result, many developers use Python to speed up the creation of data-driven web applications. One of the Python frameworks which is Specially dedicated to Web Applications is Web2py. Web2Py is a Full Stack Python web framework that includes all of the tools, components, and APIs needed to create a fully working online application.
Web2Py makes it easier to create data-driven web applications by supporting multiple popular relational and NoSQL databases. And sessions in web2py are also very important, so in this blog, we will learn about Sessions in web2py and important Methods in Sessions in web2py.

Sessions in web2py
The session is another instance of the Storage class. Classes declared in modules are likewise in the grey region and should not be stored. They usually function, but they can break. Because session is a Storage object, attempting to access an attribute/key that has not been set results in None rather than an error.
For example, if the web server is restarted and a user gets a session, this may occur before the module is imported. When the web server begins a new worker process, the same issue occurs. The same issue exists in a distributed setting.
Syntax:
session.myvariable = "hello"
can be retrieved at a subsequent time:
a = session.myvariable
As long as the code is performed by the same user during the same session. Because the session is a Storage object, attempting to access an attribute/key that has not been set results in None rather than an error.
Serialization preserves variables stored in the session between requests.
When user classes are not yet specified, sessions are obtained before an execution environment for the request is built, that is before any model or controller is run. As a result, user-defined classes cannot be pickled.
There are three significant methods for the session object.
- Forget
- Secure
-
Connect

Forget
It instructs web2py not to store the session. This should be used in controllers whose actions are often invoked and do not need tracking of user activity.
Syntax:
session.forget()
session.forget() prohibits writing to the session file, regardless of whether it has been updated. session.forget(response) additionally unlocks and closes the session file. This function is rarely used since sessions are not stored when they are not updated.
However, if the website performs numerous Ajax calls simultaneously, it is a good idea for the Ajax actions to use session.forget(response). Otherwise, each Ajax operation must wait for the preceding one to finish (and unlock the session file) before proceeding, slowing down page loading. It is worth noting that sessions are not locked when they are saved in the database.

Secure
This instructs web2py to make the session cookie a secure cookie. If the app is using HTTPS, this should be set.
Syntax:
session.secure()
By setting the session cookie to secure, the server instructs the browser not to send the session cookie back to the server unless the connection is encrypted using HTTP.
The second option is to connect. Sessions are typically saved to the filesystem, and a session cookie is used to store and retrieve the session.id. Using the connect Method, you may instruct web2y to save sessions in the database or in cookies, removing the need to access the disc for session management.

Connect
It instructs web2py to keep the sessions in the database rather than on the disc.
Syntax:
session.connect()
Session.connect must be used after db=DAL(...) but before any other logic that relies on sessions. cPickled sessions are saved in the session_data field.
Syntax:
session.connect(request, response, cookie_key='yoursecret', compression_level=None)
The cookie key is a symmetric encryption key in this case. The compression level Zlib encryption level is optional. While sessions in cookies are frequently advised for scalability, their capacity is restricted. Large sessions will result in cookies that are broken.
Printing the request, session, and response system variables allows you to examine the status of your application at any moment. One Method is to create a dedicated action:
Separate Sessions
If you have a large number of sessions stored on the disc, file system access may create a bottleneck. The syntax is as follows:
Syntax:
session.connect(request, response, separate=True)
By specifying separate=True, web2py will store sessions in subfolders of the "sessions/" folder rather than the "sessions/" folder. The subdirectory will be created on its own. Sessions with the same prefix will be grouped together in the same subdirectory. Again, the preceding must be called before any logic that may need the session.




