43 lines
1.6 KiB
Python
43 lines
1.6 KiB
Python
import json
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
|
|
PORT = 80
|
|
LINKS_FILE = 'links.json'
|
|
|
|
class RedirectHandler(BaseHTTPRequestHandler):
|
|
def do_GET(self):
|
|
try:
|
|
with open(LINKS_FILE) as f:
|
|
links = json.load(f)
|
|
except Exception:
|
|
self.send_response(500)
|
|
self.end_headers()
|
|
self.wfile.write(b"500: Error al leer el archivo de links.")
|
|
return
|
|
|
|
key = self.path.strip("/")
|
|
if key in links:
|
|
self.send_response(302)
|
|
self.send_header('Location', links[key])
|
|
self.end_headers()
|
|
else:
|
|
self.send_response(404)
|
|
self.end_headers()
|
|
self.wfile.write(b"404: Link no encontrado.")
|
|
|
|
def log_message(self, format, *args):
|
|
return # Disable logging
|
|
|
|
if __name__ == "__main__":
|
|
print(" ___ _ _______ ______ ___ ___ __ _ ___ _ _______ ")
|
|
print("| | | || || _ | | | | | | | | || | | || |")
|
|
print("| |_| || _ || | || ____ | | | | | |_| || |_| || _____|")
|
|
print("| _|| |_| || |_||_ |____| | | | | | || _|| |_____ ")
|
|
print("| |_ | ___|| __ | | |___ | | | _ || |_ |_____ |")
|
|
print("| _ || | | | | | | || | | | | || _ | _____| |")
|
|
print("|___| |_||___| |___| |_| |_______||___| |_| |__||___| |_||_______|")
|
|
|
|
print(f"Corriendo servidor de links en el puerto {PORT}")
|
|
server = HTTPServer(('', PORT), RedirectHandler)
|
|
server.serve_forever()
|