Commit a0cca0a3 authored by AntonKras's avatar AntonKras
Browse files

Сделан редирект при вводе короткой ссылки

parent 38a8d92f
from flask import Flask
from flask import render_template, redirect, request
from flask import Flask, render_template, redirect, make_response
app = Flask(__name__)
@app.route('/', methods=['GET'])
def main():
yml_dict = extract_data_from_yml('static/urls.yml')
return render_template('index.html')
@app.route('/redir', methods=['GET'])
def redir():
print(request.base_url)
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def redirect_somewhere(path):
yml_dict = extract_data_from_yml('static/urls.yml')
return redirect("http://" + yml_dict['ah712'] + "/")
print(yml_dict)
if path in yml_dict:
return redirect("https://www." + yml_dict[path])
else:
return make_response(404)
def extract_data_from_yml(urls) -> dict:
......@@ -27,12 +29,14 @@ def extract_data_from_yml(urls) -> dict:
with open(urls, 'r') as f:
content = f.read().split('\n')
content = content[:-1]
for elem in content:
final_dict[elem.split(':')[0]] = elem.split(':')[1]
return final_dict
if __name__ == '__main__':
app.debug(True)
app.run(host='0.0.0.0')
app.run()
yah444:yahoo.com
ram124:rambler.ru
ah712:yandex.ru
gogl44:google.ru
yah444:yahoo.com
\ No newline at end of file
......@@ -5,6 +5,11 @@
<title>Title</title>
</head>
<body>
Введите ссылку в адресной строке
{#<p><b>Введите длинную ссылку:</b><br>#}
{#<form action="/add">#}
{# <input type="text" size="40">#}
{# <button>ff</button>#}
{#</form>#}
</body>
</html>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment