The "Cookie Handling with Flask" application is a Flask-based web service designed to illustrate how to work with cookies in a web application. It provides the following functionality:
- Displays the current date and time on the home page.
- Allows you to set a cookie with a specified value that expires after 5 seconds.
- Retrieves and displays the value of the previously set cookie.
The code consists of several parts, each serving a specific purpose:
- Importing Required Modules:
Flask
: This module is used for creating the web application.request
: It is used for handling incoming HTTP requests.make_response
: Used to create an HTTP response.datetime
: Provides functions for working with dates and times.time
: Helps in managing time-related operations.
-
Creating the Flask App:
app = Flask(__name__)
This line initializes the Flask application.
-
Routes:
-
Home Page:
@app.route('/') def index():
This route displays the current date and time. It fetches the current date and time, formats it, and returns it as a response.
-
Set Cookie:
@app.route('/set_cookie/<value>') def set_cookie(value):
This route sets a cookie with a specified value. It calculates the expiration time as a UNIX timestamp and sets the cookie with the provided value.
-
Get Cookie:
@app.route('/get_cookie') def get_cookie():
This route retrieves and displays the value of the previously set cookie.
- Configuration:
hostMachine = "127.0.0.1"
portNo = 5000
debugMode = True
These variables store the configuration settings for the Flask application, such as the host, port, and debug mode.
- Starting the Application:
if __name__ == "__main__":
This conditional block starts the Flask application with the specified host, port, and debug mode if the script is run directly.
- To run this application, make sure you have Flask installed. You can install Flask using
pip
:
pip install Flask
- Run the script, and the Flask application will start.
python your_script_name.py
- Access the application in a web browser:
- Home Page:
http://127.0.0.1:5000/
- Set Cookie:
http://127.0.0.1:5000/set_cookie/your_cookie_value
- Get Cookie:
http://127.0.0.1:5000/get_cookie
- The cookie set using the
/set_cookie
route will expire after 5 seconds.