Un espacio de difusión Tecnológica

Un servidor Web en 30 líneas de Código !!

 Te haz preguntado como programar un servidor web en pocas líneas de código en Boo, el cual es un lenguaje que comenzó como experimento inspirado en Python, y que se perfila en un lenguaje mucho más potente, dada la comunidad que le acompaña.

Pues leyendo un interesante articulo de Oren Eini…. Aquí te lo presento!!

import System.Net
import System.IO

if argv.Length != 2:
        print “You must pass [prefix] [path] as parameters”
        return

prefix = argv[0]
path = argv[1]

if not Directory.Exists(path):
        print “Could not find ${path}”
        return

listener = HttpListener()
listener.Prefixes.Add(prefix)
listener.Start()

while true:
        context = listener.GetContext()
        file = Path.GetFileName(context.Request.RawUrl)
        fullPath = Path.Combine(path, file)
        if File.Exists(fullPath):
               context.Response.AddHeader(“Content-Disposition”,”attachment; filename=${file}”)
               bytes = File.ReadAllBytes(fullPath)
               context.Response.OutputStream.Write(bytes, 0, bytes.Length)
               context.Response.OutputStream.Flush()
               context.Response.Close()
        else:
               context.Response.StatusCode = 404
               context.Response.Close()

  para compilar :

# booi prueba.boo http://localhost:8085/ ~/Desktop/

Una respuesta

  1. Pingback: Entradas en las blogosferas.73 - Carrero Bitácora de los Hermanos Carrero, David Carrero Fernández-Baillo y Jaime Carrero Fernández-Baillo.

Deja un comentario

Fill in your details below or click an icon to log in:

Logo de WordPress.com

You are commenting using your WordPress.com account. Log Out / Cambiar )

Twitter picture

You are commenting using your Twitter account. Log Out / Cambiar )

Facebook photo

You are commenting using your Facebook account. Log Out / Cambiar )

Connecting to %s

Seguir

Get every new post delivered to your Inbox.