-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
38 lines (26 loc) · 1.02 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import sqlite3
from flask import Flask, redirect
host_url = 'http://localhost:5000/{}' # The template for the shortened urls.
app = Flask(__name__)
@app.route('/')
def home():
return "URL Shortner"
@app.route('/<short_url>') # Get the back-half
def redirect_to_original(short_url):
short_url = host_url.format(short_url) # Convert the back-half to url
query = f"SELECT long_url FROM urls WHERE short_url = ?" # Get its corresponding long url.
cursor.execute(query, (short_url,))
res = cursor.fetchone()
if res is not None:
long_url = res[0]
if long_url.find("http://") != 0 and long_url.find("https://") != 0:
long_url = "https://" + long_url # Complete the url if its not.
return redirect(long_url) # Redirect to the destination.
else:
return "URL not found"
def start_server():
global connection, cursor
connection = sqlite3.connect('url_data.db', check_same_thread=False)
cursor = connection.cursor()
app.run()
start_server()