Sitemap
Holyposts

Holyposts

Holyposts is a technology blog that offers its readers a wide range of articles, reviews and tutorials.

Creating a URL Shortener with Python Flask

2 min readJan 29, 2023

--

Press enter or click to view image in full size
Photo by Remotar Jobs on Unsplash

Creating a URL shortener using Python Flask is a fun and simple project that can help you dive into the world of web development. Flask is a lightweight web framework for Python that allows you to build web applications quickly and easily. In this article, we’ll walk through the process of building a basic URL shortener using Flask.

Step 1: Set Up a Flask Application

To get started, create a new directory for your project and open a terminal in that directory. Then, run the following command to create a new virtual environment for your project:

# For windows:
virtualenv venv
# For linux:
python3 -m venv venv

Activate the virtual environment by running the following command:

# For windows
venv/Scripts/activate
# For linux
source venv/bin/activate

Next, install Flask by running the following command:

pip install Flask

Create a new file in your project directory named app.py and add the following code to set up a basic Flask application:

from flask import Flask, request, redirect

app = Flask(__name__)

@app.route('/')
def index():
return 'Welcome to the URL Shortener'

if __name__ == '__main__':
app.run(debug=True)

Get Holyposts’s stories in your inbox

Join Medium for free to get updates from this writer.

Run the following command to start the Flask development server:

python app.py

Visit http://localhost:5000 in your web browser to see the "Welcome to the URL Shortener" message.

Step 2: Store URLs in a Dictionary

Next, we’ll store the original URLs and their corresponding shortened URLs in a Python dictionary. Add the following code to your app.py file:

url_map = {}

@app.route('/shorten', methods=['POST'])
def shorten_url():
original_url = request.form['url']
short_url = generate_short_url(https://medium.com/original_url)
url_map[short_url] = original_url
return short_url

def generate_short_url(https://medium.com/original_url):
# Generate a unique short URL for the original URL
# (We'll implement this in the next step)
pass

Step 3: Generate Unique Short URLs

To generate unique short URLs, we’ll use a hash function. A hash function takes an input (in this case, the original URL) and returns a fixed-length output (the short URL). We’ll use the SHA-1 hash function, which is part of the Python hashlib library. Add the following code to your app.py file:

import hashlib

def generate_short_url(https://medium.com/original_url):
hash = hashlib.sha1(original_url.encode())
short_url = hash.hexdigest()[:8]
return short_url

Step 4: Redirect to the Original URL

Finally, we’ll add a route to redirect users to the original URL when they visit the shortened URL. Add the following code to your app.py file:

@app.route('/<short_url>')
def redirect_url(https://medium.com/short_url):
original_url = url_map.get(short_url)
if original_url:
return redirect(original_url

--

--

Holyposts
Holyposts

Written by Holyposts

0 followers

Holyposts is a technology blog that offers its readers a wide range of articles, reviews and tutorials.