From 8f2ea58a01d185df8d96010cf996287549e0ff3a Mon Sep 17 00:00:00 2001 From: Bruno Barbosa Date: Fri, 9 Sep 2011 00:17:25 -0300 Subject: [PATCH] =?UTF-8?q?Adicionado=20m=C3=A9todo=20para=20remover=20o?= =?UTF-8?q?=20http://=20dos=20links?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GetHeader.py | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/GetHeader.py b/GetHeader.py index 239b214..e9ab7cb 100644 --- a/GetHeader.py +++ b/GetHeader.py @@ -14,24 +14,48 @@ def __init__(self, *args): def get_url(self): return self._url + def set_url(self, link): + self._url = link + def get_error(self, num): erros = {1:"Falha na conexão: O host conectado não respondeu!"} return erros[num] + def __remove_http(self): + """ + Remove o HTTP:// antes do link para evitar erros na conexão + """ + links = self.get_url() + new_link = [] + for link in links: + if "http://" in link: + new_link.append(link[7:]) + self.set_url(new_link) - def get_cabecalho(self): + + #TODO: Criar método para dividir o link depois do '/', se houver, para passar em 'end_url' no get_cabecalho + + + def get_cabecalho(self, end_url="/"): + """ + Este método efetua uma conexão com os links passados, afim de obter seu código http + """ links = self.get_url() + result = {} for link in links: + # Faz a conexão em cada um dos links para buscar o cabeçalho conn = httplib.HTTPConnection(link) try: - conn.request("GET", "/") + conn.request("GET", end_url) except socket.error: return self.get_error(1) - result = conn.getresponse() + cod_http = conn.getresponse() + + result[link] = cod_http.status, cod_http.reason - return result.status, result.reason + return result