Het downloaden van klingeltöne, Download von klingeltöne, Het downloaden van klingeltöne, Descargar tonos, Téléchargez des sonneries, scarica suonerie, Beltonen downloaden, Nedlasting av ringetoner, Download ringtones

Un servidor web en 30 líneas de código

Interesante este post de Oren Eini. Es un código escrito en Boo (lenguaje tipo Python para .NET):

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()

Y ejecutando el comando:

$ booi prueba.boo http://localhost:8085/ ~/Escritorio/

tenemos funcionando un webserver localmente. Me acuerdo como en una de las charlas de alguna de las jornadas Python en Santa Fe el disertante hacía algo similar. La verdad, muy útil este tipo de código.

Google
 

  • http://nacho.larrateguy.com.ar Nacho

    Acordate el TP que hicimos para SOA, con sockets… Imaginate hacer un mini HTTPserver con C… Asustar’ia m’as que este c’odigo…
    Boo!
    Seguro nos va a ser ‘util este snippet :-)