Git Product home page Git Product logo

flask-app_cookies's Introduction

Cookie Handling with Flask Application

Overview

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:

  1. Displays the current date and time on the home page.
  2. Allows you to set a cookie with a specified value that expires after 5 seconds.
  3. Retrieves and displays the value of the previously set cookie.

Code Structure

The code consists of several parts, each serving a specific purpose:

  1. 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.
  1. Creating the Flask App:

    app = Flask(__name__) 

    This line initializes the Flask application.

  2. 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.

  1. 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.

  1. 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.

Usage

  1. To run this application, make sure you have Flask installed. You can install Flask using pip:
pip install Flask
  1. Run the script, and the Flask application will start.
python your_script_name.py
  1. 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

Note

  • The cookie set using the /set_cookie route will expire after 5 seconds.

flask-app_cookies's People

Contributors

sunilmanjhu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.