Skip to content

Commit 3dfcc3d

Browse files
authoredSep 6, 2024
Python and HTML files
1 parent 85bad1a commit 3dfcc3d

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
 

‎index.html

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!DOCTYPE html>
2+
<html lang="es">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Geolocalización</title>
7+
</head>
8+
<body>
9+
<h1>Obtener Ubicación</h1>
10+
<button id="getLocation">Obtener Ubicación</button>
11+
<p id="result"></p>
12+
13+
<script>
14+
// JavaScript para obtener la ubicación
15+
document.getElementById('getLocation').addEventListener('click', function() {
16+
if (navigator.geolocation) {
17+
navigator.geolocation.getCurrentPosition(
18+
function(position) {
19+
// Éxito en la obtención de la posición
20+
const latitude = position.coords.latitude;
21+
const longitude = position.coords.longitude;
22+
document.getElementById('result').textContent = `Latitud: ${latitude}, Longitud: ${longitude}`;
23+
console.log(`Latitud: ${latitude}, Longitud: ${longitude}`);
24+
},
25+
function(error) {
26+
// Manejo de errores
27+
switch(error.code) {
28+
case error.PERMISSION_DENIED:
29+
document.getElementById('result').textContent = 'Permiso denegado por el usuario.';
30+
break;
31+
case error.POSITION_UNAVAILABLE:
32+
document.getElementById('result').textContent = 'Información de ubicación no disponible.';
33+
break;
34+
case error.TIMEOUT:
35+
document.getElementById('result').textContent = 'La solicitud para obtener la ubicación ha caducado.';
36+
break;
37+
case error.UNKNOWN_ERROR:
38+
document.getElementById('result').textContent = 'Error desconocido.';
39+
break;
40+
}
41+
console.error(`Error ${error.code}: ${error.message}`);
42+
}
43+
);
44+
} else {
45+
document.getElementById('result').textContent = 'La geolocalización no es soportada por este navegador.';
46+
}
47+
});
48+
</script>
49+
</body>
50+
</html>

‎server.py

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import http.server
2+
import socketserver
3+
import socket
4+
from datetime import datetime
5+
import requests
6+
7+
def print_ascii_art():
8+
ascii_art = """
9+
_____
10+
.-' `-.
11+
.' `.
12+
/ _.._ _.._ \\
13+
| / \\ / \\ |
14+
| \\ / \\ / |
15+
\\ `--' `--' /
16+
`. .'
17+
`-._ _.-'
18+
`"`
19+
Creado por DST
20+
GitHub: https://github.com/DISTinTheHouse
21+
"""
22+
print(ascii_art)
23+
24+
print_ascii_art()
25+
26+
PORT = 8000
27+
28+
# Detecta auto IP local
29+
def get_local_ip():
30+
hostname = socket.gethostname()
31+
local_ip = socket.gethostbyname(hostname)
32+
return local_ip
33+
34+
# IP Pública
35+
def get_public_ip():
36+
try:
37+
response = requests.get('https://api.ipify.org?format=json')
38+
ip_data = response.json()
39+
return ip_data['ip']
40+
except requests.RequestException as e:
41+
print(f"Error al obtener IP pública: {e}")
42+
return None
43+
44+
# Establece la carpeta en la que estás sirviendo los archivos (en este caso, la misma que el script)
45+
DIRECTORY = "."
46+
47+
# Crea un manejador que sirva los archivos HTML, CSS, JS en la carpeta actual
48+
class CustomHandler(http.server.SimpleHTTPRequestHandler):
49+
def __init__(self, *args, **kwargs):
50+
super().__init__(*args, directory=DIRECTORY, **kwargs)
51+
52+
def log_message(self, format, *args):
53+
client_ip = self.client_address[0]
54+
public_ip = get_public_ip()
55+
user_agent = self.headers.get('User-Agent', 'Desconocido')
56+
accept_language = self.headers.get('Accept-Language', 'Desconocido')
57+
timezone = self.headers.get('Time-Zone', 'Desconocido')
58+
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
59+
60+
log_entry = (
61+
f"Timestamp: {timestamp}\n"
62+
f"IP: {client_ip}\n"
63+
f"IP Pública: {public_ip}\n"
64+
f"usuario-agente: {user_agent}\n"
65+
f"Accept-Language: {accept_language}\n"
66+
f"Time-Zone: {timezone}\n"
67+
"----------------------------------------\n"
68+
)
69+
70+
# Registra detalles en un archivo .txt
71+
with open("access_log.txt", "a") as log_file:
72+
log_file.write(log_entry)
73+
74+
# Y en consola
75+
print(log_entry)
76+
77+
# Obtén la IP local
78+
local_ip = get_local_ip()
79+
80+
# Configura el servidor HTTP
81+
with socketserver.TCPServer((local_ip, PORT), CustomHandler) as httpd:
82+
print(f"Sirviendo en http://{local_ip}:{PORT}")
83+
print("Presiona Ctrl+C para detener el servidor.")
84+
# Ejecuta el servidor hasta que lo detengas manualmente
85+
httpd.serve_forever()

0 commit comments

Comments
 (0)
Please sign in to comment.