Home > Programación, Python > Simple Server y Proxy

Simple Server y Proxy

Python LogoLa otra vez navegando me encontre con un simple código en Python que permite compartir archivos via web, esta muy bueno cuando no tenes ganas de configurar el samba o bien si queres que tu amigo de China vea lo que tenes en dicho directorio.

Lo que hace es montarte un mini servidor en la carpeta que lo pongas y configurandole el puerto de ejecución.

A continuación les paso un par de ejemplos

Se dirigen a una carpeta y luego typean en su consola bash preferida:

python -m SimpleHTTPServer 8000

Nótese el último parámetro, el 8000 es el puerto donde lo quieren abrir, si ya tienen ocupado el 8000 basta cambiar de puerto y listo.

Bien ahora hagamos un mini script que nos permita hacer lo mismo.

Creamos un archivo compartir.py y le copiamos lo siguiente.

##Montando un mini servidor de archivos

import SimpleHTTPServer
import SocketServer

PORT = 8000 #cambiar de puerto si lo desean

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer((“”, PORT), Handler)

print “Escuchando el puerto: “, PORT
httpd.serve_forever()

Ahora lo ejecutamos:

$ python compartir.py
Escuchando el puerto: 8000
localhost - - [11/Oct/1999 15:07:44] code 403, message Directory listing
not supported
localhost - - [11/Oct/1999 15:07:44] "GET / HTTP/1.1" 403 -
localhost - - [11/Oct/1999 15:07:56] "GET /samples/sample.htm HTTP/1.1" 200 -

Ahora para ver que funciona, solo tenemos que abrir un navegador cualquiera y tipear http://ip_compartida_con_archivo:PUERTO

Con esto ya terminamos la parte del servidor, pero como el titulo lo indica tambien les voy a explicar como hacer un mini servidor proxy, asi se pueden conectar desde su trabajo con la IP de su casa, o bien su amigo de China que les filtran los puertos pueden hacerlo conectar desde la Ip de Uds. y asi el puede navegar Felizmente.

Otra utilidad interesante es para compartir internet desde dos PCs conectadas por LAN.

El código es el siguiente.

Creamos un archivo proxy.py que va a contener:

import SocketServer
import SimpleHTTPServer
import urllib

PORT = 1337

class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
self.copyfile(urllib.urlopen(self.path), self.wfile)

httpd = SocketServer.ForkingTCPServer(('', PORT), Proxy)
print "serving at port", PORT
httpd.serve_forever()

Para probrar esto solo hace falta configurar el navegador con el IP de donde se este ejecutando el archivo python y el puerto….


German Programación, Python , ,

  1. January 1st, 2010 at 10:05 | #1

    I wanna get screen protectors and was wondering if the iphone 2g had the same size as the ipod touch. Thanks
    unlock iphone

  2. January 4th, 2010 at 12:04 | #2

    I recently got a practise account for Forex Trader and i would like to try out Megadroid Forex to see if it would be worth getting with a actual account on Forex can you give me a web link where i can get a free trial of Megadroid Forex for my practise account?
    best forex software

  3. January 4th, 2010 at 18:17 | #3

    Looking for an experienced or cited answer as to what the most consistent and widely used indicators are for the forex markets, and what the most popular time frame is. I usually trade the 1 hour, but I’m not sure if that is best or not.
    best forex software

  4. January 6th, 2010 at 15:41 | #4

    Hi,

    I am Mark, soon 31 years , I teach sports in high school

    bye bye,

    Mark, my little blog moto

  1. No hay trackbacks.