from flask import Flask, request, redirect
import string
import random
app = Flask(__name__)
url_database = {}
def generate_short_url():
chars = string.ascii_letters + string.digits
return ''.join(random.choice(chars) for _ in range(6))
@app.route('/', methods=['GET', 'POST'])
def shorten_url():
if request.method == 'POST':
url = request.form['url']
short_url = generate_short_url()
url_database[short_url] = url
return f"Short URL: {request.host_url}{short_url}"
return '''
<form method="post">
<label for="url">Enter URL to shorten:</label>
<input type="text" name="url" id="url" required>
<button type="submit">Shorten URL</button>
</form>
'''
@app.route('/<short_url>')
def redirect_to_url(short_url):
url = url_database.get(short_url)
if url is None:
return f"Invalid short URL: {short_url}"
return redirect(url)
if __name__ == '__main__':
app.run(debug=True)
Комментарий недоступен
Понятно,попробую изменить.Если можешь покажи отредактированный код.