From 22da70ac3970bb0d919df9d31cfdad287147ce58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9munaire?= Date: Wed, 7 Nov 2012 16:37:19 +0100 Subject: [PATCH] Use urllib instead of own tools for modules ycc and networking --- modules/ddg/__init__.py | 2 +- modules/networking.py | 64 +++++++++++++++++++++++++++++++++++------ modules/ycc.py | 25 +++++++++------- 3 files changed, 70 insertions(+), 21 deletions(-) diff --git a/modules/ddg/__init__.py b/modules/ddg/__init__.py index 977c6db..455b4f2 100644 --- a/modules/ddg/__init__.py +++ b/modules/ddg/__init__.py @@ -134,5 +134,5 @@ def wiki(msg): return res else: return Response(msg.sender, - "No information about " + msg.cmds[1], + "No information about " + " ".join(msg.cmds[1:]), msg.channel) diff --git a/modules/networking.py b/modules/networking.py index e1a6d1d..bac15ba 100644 --- a/modules/networking.py +++ b/modules/networking.py @@ -1,6 +1,9 @@ # coding=utf-8 -from tools import web +import http.client +import json +from urllib.parse import urlparse +from urllib.request import urlopen nemubotversion = 3.3 @@ -21,11 +24,8 @@ def cmd_traceurl(msg): if 1 < len(msg.cmds) < 6: res = list() for url in msg.cmds[1:]: - if web.isURL(url): - trace = web.traceURL(url) - res.append(Response(msg.sender, trace, channel=msg.channel, title="TraceURL")) - else: - res.append(Response(msg.sender, "%s n'est pas une URL valide" % url, channel=msg.channel)) + trace = traceURL(url) + res.append(Response(msg.sender, trace, channel=msg.channel, title="TraceURL")) return res else: return Response(msg.sender, "Indiquer une URL a tracer !", channel=msg.channel) @@ -34,9 +34,12 @@ def cmd_isup(msg): if 1 < len(msg.cmds) < 6: res = list() for url in msg.cmds[1:]: - host = web.getHost(url) - if host is not None: - isup = web.getJSON("http://isitup.org/" + host + ".json") + o = urlparse(url, "http") + if o.netloc == "": + o = urlparse("http://" + url) + if o.netloc != "": + raw = urlopen("http://isitup.org/" + o.netloc + ".json", timeout=10) + isup = json.loads(raw.read().decode()) if "status_code" in isup and isup["status_code"] == 1: res.append(Response(msg.sender, "%s est accessible (temps de reponse : %ss)" % (isup["domain"], isup["response_time"]), channel=msg.channel)) else: @@ -46,3 +49,46 @@ def cmd_isup(msg): return res else: return Response(msg.sender, "Indiquer une URL a verifier !", channel=msg.channel) + + +def traceURL(url, timeout=5, stack=None): + """Follow redirections and return the redirections stack""" + if stack is None: + stack = list() + stack.append(url) + + o = urlparse(url, "http") + if o.netloc == "": + return stack + if o.scheme == "http": + conn = http.client.HTTPConnection(o.netloc, port=o.port, timeout=timeout) + else: + conn = http.client.HTTPSConnection(o.netloc, port=o.port, timeout=timeout) + try: + conn.request("HEAD", o.path, None, {"User-agent": "Nemubot v3"}) + except socket.timeout: + stack.append("Timeout") + return stack + except socket.gaierror: + print (" Unable to receive page %s from %s on %d." + % (o.path, o.netloc, o.port)) + return None + + try: + res = conn.getresponse() + except http.client.BadStatusLine: + return None + finally: + conn.close() + + if res.status == http.client.OK: + return stack + elif res.status == http.client.FOUND or res.status == http.client.MOVED_PERMANENTLY or res.status == http.client.SEE_OTHER: + url = res.getheader("Location") + if url in stack: + stack.append(url) + return stack + else: + return traceURL(url, timeout, stack) + else: + return None diff --git a/modules/ycc.py b/modules/ycc.py index 792775d..285a817 100644 --- a/modules/ycc.py +++ b/modules/ycc.py @@ -1,8 +1,8 @@ # coding=utf-8 import re - -from tools import web +from urllib.parse import urlparse +from urllib.request import urlopen nemubotversion = 3.3 @@ -39,11 +39,14 @@ def cmd_ycc(msg): if len(msg.cmds) < 6: res = list() for url in msg.cmds[1:]: - srv = web.getHost(url) - if srv is not None: - res.append(gen_response( - web.getURLContent("http://ycc.fr/redirection/create/" - + url).decode(), msg, srv)) + o = urlparse(url, "http") + if o.scheme != "": + raw = urlopen("http://ycc.fr/redirection/create/" + url, + timeout=10) + if o.netloc == "": + res.append(gen_response(raw.read().decode(), msg, o.scheme)) + else: + res.append(gen_response(raw.read().decode(), msg, o.netloc)) else: res.append(gen_response(False, msg, url)) return res @@ -56,13 +59,13 @@ def parselisten(msg): res = re.match(".*([a-zA-Z0-9+.-]+):(//)?([^ ]*).*", msg.content) if res is not None: url = res.group(1) - srv = web.getHost(url) - if srv is not None: - if srv == "ycc.fr": + o = urlparse(url) + if o.scheme != "": + if o.netloc == "ycc.fr": return False if msg.channel not in LAST_URLS: LAST_URLS[msg.channel] = list() - LAST_URLS[msg.channel].append(url) + LAST_URLS[msg.channel].append(o.geturl()) return True return False