Last active
May 2, 2020 00:13
-
-
Save tomasdelvechio/0250ebaba64c0b743981bc95ec98a2ab to your computer and use it in GitHub Desktop.
HTTP Servers de Ejemplo para TP - HTTP de TyR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
import http.server | |
HOST_NAME = 'localhost' | |
PORT = 8000 | |
class Handler(http.server.BaseHTTPRequestHandler): | |
def do_GET(server): | |
"""Respondo a una petición de tipo GET""" | |
# Imprimo los encabezados | |
print('-' * 80) | |
print(server.command, server.path, server.request_version) | |
print(server.headers) | |
# Devuelvo la respuesta | |
server.send_response(200) | |
server.send_header('Content-Type', 'text/html') | |
server.end_headers() | |
server.wfile.write(b'<html><head><title>Pagina HTML de ejemplo</title>' | |
b'</head><body><p>Esta es una prueba, con texto en <b>negrita</b>,' | |
b'<i>cursiva</i> e incluso una imagen externa:</p>' | |
b'<img src="http://www.labredes.unlu.edu.ar/themes/glossyblue/images/' | |
b'header-bg.jpg" /></body></html>') | |
def log_message(*args): | |
""" Deshabilito la salida por defecto del servidor """ | |
pass | |
if __name__ == '__main__': | |
http_server = http.server.HTTPServer((HOST_NAME, PORT), Handler) | |
print('Ejecutando Server HTTP - http://%s:%s' % (HOST_NAME, PORT)) | |
print('Presione Ctrl+C para detener...') | |
try: | |
http_server.serve_forever() | |
except KeyboardInterrupt: | |
pass | |
http_server.server_close() | |
print('Deteniendo Server HTTP - %s:%s' % (HOST_NAME, PORT)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
import http.server | |
HOST_NAME = 'localhost' | |
PORT = 8000 | |
def detectar_so(user_agent): | |
# Ver listados en http://www.useragentstring.com/pages/useragentstring.php | |
if 'Linux' in user_agent: | |
return b'Veo que Ud. esta usando Linux como S.O.' | |
elif 'Windows' in user_agent: | |
return b'Veo que Ud. esta usando Windows como S.O.' | |
else: | |
return b'No conozco su S.O.' | |
def get_pagina_ok(): | |
""" Función que dvuelve la página de exito de ejemplo """ | |
# Como alternativa podría abrirse un archivo del disco, leerlo y devolverlo | |
# como cadena de texto. | |
return (b'<html><head><title>Pagina HTML de ejemplo</title>' | |
b'</head><body><p>Esta es una prueba, con texto en <b>negrita</b>,' | |
b'<i>cursiva</i> e incluso una imagen externa:</p>' | |
b'<img src="http://www.labredes.unlu.edu.ar/themes/glossyblue/images' | |
b'/header-bg.jpg" />') | |
class Handler(http.server.BaseHTTPRequestHandler): | |
def do_GET(server): | |
"""Respondo a una petición de tipo GET""" | |
# Imprimo los encabezados por consola | |
print('-' * 80) | |
print(server.command, server.path, server.request_version) | |
print(server.headers) | |
# Devuelvo la respuesta | |
if server.path.startswith('/ir_a/'): | |
ir_a = server.path.split('/')[-1] | |
server.send_response(302) | |
server.send_header('Location', 'http://' + ir_a) | |
server.end_headers() | |
elif server.path.startswith('/no_existe'): | |
server.send_response(404) | |
server.send_header('Content-Type', 'text/plain') | |
server.end_headers() | |
server.wfile.write('Pagina no encontrada') | |
else: | |
server.send_response(200) | |
server.send_header('Content-Type', 'text/html') | |
server.end_headers() | |
server.wfile.write(get_pagina_ok()) | |
server.wfile.write(bytes(b'<p>' + detectar_so(server.headers['User-Agent']))) | |
server.wfile.write(b'</p></body></html>') | |
def log_message(*args): | |
""" Deshabilito la salida por defecto del servidor """ | |
pass | |
if __name__ == '__main__': | |
http_server = http.server.HTTPServer((HOST_NAME, PORT), Handler) | |
print('Ejecutando Server HTTP - http://%s:%s' % (HOST_NAME, PORT)) | |
print('Presione Ctrl+C para detener...') | |
try: | |
http_server.serve_forever() | |
except KeyboardInterrupt: | |
pass | |
http_server.server_close() | |
print('Deteniendo Server HTTP - %s:%s' % (HOST_NAME, PORT)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment