Add HTTP server with CORS support, cookie management, and real-time cookie viewer

This commit is contained in:
2025-02-16 23:32:36 +01:00
parent 28f9d2eedf
commit e2f6f2e82c
4 changed files with 2749 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
import json
import time
import pandas as pd
from playwright.sync_api import sync_playwright
# Archivo donde se guardarán las cookies
COOKIES_FILE = "cookies.json"
def save_cookies(context):
"""Guarda las cookies actuales en un archivo JSON."""
cookies = context.cookies()
df = pd.DataFrame(cookies)
# Convertir a JSON
with open(COOKIES_FILE, "w", encoding="utf-8") as f:
json.dump(cookies, f, indent=4, ensure_ascii=False)
print(f"Cookies guardadas en {COOKIES_FILE}")
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto("https://xataka.com")
print("Navegador abierto. Puedes usarlo libremente.")
try:
while True:
save_cookies(context)
time.sleep(10) # Espera 10 segundos antes de actualizar las cookies
except KeyboardInterrupt:
print("\nCerrando navegador...")
browser.close()
+2642
View File
File diff suppressed because it is too large Load Diff
+13
View File
@@ -0,0 +1,13 @@
from http.server import HTTPServer, SimpleHTTPRequestHandler
class CORSRequestHandler(SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header("Access-Control-Allow-Origin", "*") # Permitir solicitudes de cualquier origen
self.send_header("Access-Control-Allow-Methods", "GET, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type")
super().end_headers()
# Servidor en el puerto 8000
httpd = HTTPServer(("0.0.0.0", 8000), CORSRequestHandler)
print("Servidor corriendo en http://localhost:8000/")
httpd.serve_forever()
+60
View File
@@ -0,0 +1,60 @@
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Monitor de Cookies</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; }
table { width: 80%; margin: 20px auto; border-collapse: collapse; }
th, td { border: 1px solid black; padding: 10px; text-align: left; }
th { background-color: #f2f2f2; }
</style>
</head>
<body>
<h1>Cookies en tiempo real</h1>
<table>
<thead>
<tr>
<th>Dominio</th>
<th>Nombre</th>
<th>Valor</th>
</tr>
</thead>
<tbody id="cookiesTable">
<tr><td colspan="3">Cargando cookies...</td></tr>
</tbody>
</table>
<script>
async function loadCookies() {
try {
const response = await fetch("cookies.json?_=" + new Date().getTime()); // Forzar recarga evitando caché
if (!response.ok) throw new Error("Error al cargar cookies.json");
const cookies = await response.json();
const tableBody = document.getElementById("cookiesTable");
tableBody.innerHTML = ""; // Limpiar la tabla
cookies.forEach(cookie => {
const row = `<tr>
<td>${cookie.domain}</td>
<td>${cookie.name}</td>
<td>${cookie.value}</td>
</tr>`;
tableBody.innerHTML += row;
});
} catch (error) {
console.error("Error cargando cookies:", error);
document.getElementById("cookiesTable").innerHTML = "<tr><td colspan='3'>Error al cargar cookies</td></tr>";
}
}
setInterval(loadCookies, 5000);
loadCookies();
</script>
</body>
</html>