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 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
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'
- To create a cookie that will expire in one day, use:
response.cookies['cookie']['expires'] = 24 * 3600
- If you want the cookie to be available from other functions/controllers, you must specify its path:
response.cookies['cookie']['path'] = '/'
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
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
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'] = "/"
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"
The preceding can be useful if, for example, You want the user to be able to log in across subdomains.






