elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.
 
Inicio Ayuda Buscar Ingresar Registrarse
29 Mayo 2012, 05:14  


Tema destacado: Únete al Grupo Steam elhacker.NET

+  Foro de elhacker.net
|-+  Programación
| |-+  Scripting (Moderadores: Novlucker, Leo Gutiérrez., EleKtro H@cker)
| | |-+  [Python] Goo - Acorta tu URL
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [Python] Goo - Acorta tu URL  (Leído 272 veces)
The Swash

Conectado Conectado

Mensajes: 104


Programmer


Ver Perfil WWW
[Python] Goo - Acorta tu URL
« en: 24 Octubre 2011, 06:37 »

Código
#----------------------------------------------------------
# Obtener URL acortada mediante http://goo.gl
# Programador: The Swash
# Agradecimientos: Psymera, 11Sep, [Zero] y todo h-sec
# Website: http://h-sec.org
#----------------------------------------------------------
 
import socket, urllib, re
def GetGooURL(url):
   header = ['POST /api/shorten HTTP/1.1\r\n',
             'Host: goo.gl\r\n',
             'Content-Type: application/x-www-form-urlencoded;charset=utf-8\r\n',
             'Content-Length: 41\r\n',
             'Connection: close\r\n\r\n',
             'URLENCODE']
   if re.match('^http://', url):
       url2 = url
   else:
       url2 = 'http://' + url
   address = socket.gethostbyname('goo.gl')
   link = urllib.urlencode({'url':url2})
   sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   header[5] = link + '&security_token\r\n'
   length = len(header)
   try:
       sock.connect((address,80))
       for i in range(length):
           sock.send(header[i])
       buff = sock.recv(1024)
   except:
       return 'Error de conexion'
 
   sock.close()
   data = re.findall('Location: (.+)\r\n', buff)
   return data[0]
 
url = GetGooURL('h-sec.org')
print url
raw_input()

Hola amigos, les traigo este pequeño código que me llevó largo rato por los encabezados HTTP. Como dijo 11Sep puede acortarse mucho usando urllib, pero en definitiva no era mi intención.

Cualquier critica constructiva es bien recibida.

Un saludo.


En línea
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
[Python] Curso de Python con Interfaces graficas TK « 1 2 »
Scripting
Erik# 18 12,570 Último mensaje 12 Septiembre 2010, 02:27
por Dreykon
(Python)Existen ventanas de entrada y salida de datos en python
Scripting
tonilogar 11 4,346 Último mensaje 29 Noviembre 2009, 00:49
por tonilogar
Windows 7 acorta la vida de las baterías
Noticias
Novlucker 0 447 Último mensaje 1 Febrero 2010, 12:36
por Novlucker
[python]VideoTraining Aprende A Programar En Python desde 0 « 1 2 »
Scripting
juh 22 5,571 Último mensaje 16 Octubre 2010, 13:12
por sheevalum
Twitter acorta las direcciones URL automáticamente
Noticias
wolfbcn 0 478 Último mensaje 8 Junio 2011, 17:39
por wolfbcn
Powered by SMF 1.1.16 | SMF © 2006-2008, Simple Machines