-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShortUrlServer.py
99 lines (93 loc) · 3.73 KB
/
ShortUrlServer.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import http.server
import requests
import urllib
import os
import threading
import re
from socketserver import ThreadingMixIn
myDict={}
def urlify(s):
# Remove all non-word characters (everything except numbers and letters)
s = re.sub(r"[^\w\s]", '', s)
# Replace all runs of whitespace with a single dash
s = re.sub(r"\s+", '-', s)
return s
class ThreadHTTPServer(ThreadingMixIn, http.server.HTTPServer):
"This is an HTTPServer that supports thread-based concurrency."
class handler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
form='''<!DOCTYPE html><title>Shorten your URL</title><form method="POST" action="http://localhost:8000/"><textarea rows="3" cols="50" placeholder="Enter here the url" name="longURI"></textarea><br><textarea value="asd" rows="3" cols="50" placeholder="Enter here the new short name you want..."name="shortTxt"></textarea><br><button type="submit">Cut it!</button></form>'''
if self.path=="/emptyValues":
self.send_response(400)
self.send_header("Content-type","text/html; charset:utf-8")
self.end_headers()
for key in myDict.keys():
form+='''<a href="'''+key+'''">http://localhost:8000/'''+myDict[key]+'''</a><br>'''
form+="\n Please enter all values."
self.wfile.write(form.encode())
elif self.path=="/invalidURI":
self.send_response(404)
self.send_header("Content-type","text/html; charset:utf-8")
self.end_headers()
for key in myDict.keys():
form+='''<a href="'''+key+'''">http://localhost:8000/'''+myDict[key]+'''</a><br>'''
form+="\n The URL you added is invalid."
self.wfile.write(form.encode())
elif self.path=="/withLink":
self.send_response(200,'OK')
self.send_header("Content-type","text/html; charset:utf-8")
self.end_headers()
for key in myDict.keys():
form+='''<a href="'''+key+'''">http://localhost:8000/'''+myDict[key]+'''</a><br>'''
self.wfile.write(form.encode())
elif self.path=="/":
self.send_response(200,'OK')
self.send_header("Content-type","text/html; charset:utf-8")
self.end_headers()
for key in myDict.keys():
form+='''<a href="'''+key+'''">http://localhost:8000/'''+myDict[key]+'''</a><br>'''
self.wfile.write(form.encode())
else:
req=self.path[1:]
if req in myDict.values():
self.send_response(303)
key = list(myDict.keys())[list(myDict.values()).index(req)]
self.send_header('Location', key)
self.end_headers()
else:
self.send_response(200,'OK')
self.send_header("Content-type","text/html; charset:utf-8")
self.end_headers()
for key in myDict.keys():
form+='''<a href="'''+key+'''">http://localhost:8000/'''+myDict[key]+'''</a><br>'''
self.wfile.write(form.encode())
def do_POST(self):
data=self.rfile.read(int(self.headers.get('Content-length', 0))).decode()
querries=urllib.parse.parse_qs(data)
if ('longURI' in querries) and ('shortTxt' in querries):
try:
r = requests.get(querries['longURI'][0])
if r.status_code==200:
querries['shortTxt'][0]=urlify(querries['shortTxt'][0])
re=urllib.parse.urlparse(querries['longURI'][0])
myDict[querries['longURI'][0]]=querries['shortTxt'][0]
self.send_response(303)
self.send_header('Location', '/withLink')
self.end_headers()
elif r.status_code==404:
self.send_response(303)
self.send_header('Location', '/invalidURI')
self.end_headers()
except requests.exceptions.RequestException as e:
print(e)
self.send_response(303)
self.send_header('Location', '/invalidURI')
self.end_headers()
else:
self.send_response(303)
self.send_header('Location', '/emptyValues')
self.end_headers()
if __name__ == '__main__':
port = int(os.environ.get('PORT', 8000))
serv=ThreadHTTPServer(('', port),handler)
serv.serve_forever()