Skip to content

Test #645

@basilpetonline-eng

Description

@basilpetonline-eng

``from flask import Flask, request, redirect, render_template_string
import random
import string

app = Flask(name)

In-memory storage for short link mapping

url_mapping = {}

Template for the HTML form

HTML_FORM = """
<!doctype html>

<title>Birthday Wish Short Link Generator</title>

🎉 Generate a Short Link for Your Birthday Wish 🎂

Create Short Link {% if short_url %}

Your short link: {{ short_url }}

{% endif %} """

def generate_code(length=6):
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))

@app.route('/', methods=['GET', 'POST'])
def home():
short_url = None
if request.method == 'POST':
long_url = request.form['long_url']
code = generate_code()
while code in url_mapping:
code = generate_code()
url_mapping[code] = long_url
short_url = request.host_url + code
return render_template_string(HTML_FORM, short_url=short_url)

@app.route('/')
def redirect_to_long_url(code):
long_url = url_mapping.get(code)
if long_url:
return redirect(long_url)
return "

Link not found!

", 404

if name == 'main':
app.run(debug=True)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions