This is a simple URL shortener application built using Flask, a lightweight web framework in Python. The application allows users to enter a URL and generates a shorter version of it, which can be used as a redirect to the original URL.
- Accepts a long URL as input and generates a shortened version.
- Stores the mapping between the shortened URL and the original URL.
- Redirects users to the original URL when they access the shortened URL.
- Validates the input URL to ensure it is a valid URL.
- Displays success and error messages to the user.
Before running the application, make sure you have the following installed:
- Python (version 3.6 or later)
- Flask (install using
pip install flask
) - Flask-WTF (install using
pip install flask-wtf
)
-
Clone the repository or download the source code.
-
Open a terminal or command prompt and navigate to the project directory.
-
Install the required dependencies by running the following command:
pip install -r requirements.txt
-
Run the Flask application by executing the following command in the project directory:
python main.py
This will start the Flask development server on
http://localhost:5000
. -
Open a web browser and navigate to
http://localhost:5000
. -
Enter a valid URL in the input field and click the "Shorten" button.
-
If the URL is valid, the application will generate a shortened URL and display it to the user as a success message.
-
Click on the shortened URL to verify that it redirects to the original URL.
The project consists of the following files:
main.py
: Contains the Flask application code, including the routes for the index page and URL redirection. It also initializes the Flask application and sets the secret key.inputform.py
: Defines theInputForm
class, a FlaskForm subclass that represents the URL input form. It uses Flask-WTF and WTForms to define the form fields and validators.templates/index.html
: The HTML template for the index page. It includes the URL input form and displays flash messages.
- The application generates the shortened URLs using
secrets.token_urlsafe()
to ensure they are difficult to guess. However, there is still a small chance of collision due to the limited length of the generated tokens. - The
SECRET_KEY
is used to secure the session cookies and should be kept secret. It is recommended to use a strong and unique secret key in production. - The application does not currently implement any authentication or rate limiting mechanisms. Consider adding these features if the application is exposed to untrusted users or high traffic.
This project is licensed under the MIT License. See the LICENSE file for more information.