From c55e66dd704e01aaef09d3a8feea76b43609e4c4 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 8 Oct 2015 02:47:42 +0100 Subject: [PATCH] [tools/web] Add header param to getContentUrl() Add the possibility to specify headers when querying websites. --- nemubot/tools/web.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/nemubot/tools/web.py b/nemubot/tools/web.py index 8955d5d..b1d146a 100644 --- a/nemubot/tools/web.py +++ b/nemubot/tools/web.py @@ -60,7 +60,7 @@ def getPassword(url): # Get real pages -def getURLContent(url, body=None, timeout=7): +def getURLContent(url, body=None, timeout=7, header=None): """Return page content corresponding to URL or None if any error occurs Arguments: @@ -97,19 +97,24 @@ def getURLContent(url, body=None, timeout=7): else: raise IRCException("Invalid URL") + from nemubot import __version__ + if header is None: + header = {"User-agent": "Nemubot v%s" % __version__} + elif "User-agent" not in header: + header["User-agent"] = "Nemubot v%s" % __version__ + import socket try: - from nemubot import __version__ if o.query != '': conn.request("GET" if body is None else "POST", o.path + "?" + o.query, body, - {"User-agent": "Nemubot v%s" % __version__}) + header) else: conn.request("GET" if body is None else "POST", o.path, body, - {"User-agent": "Nemubot v%s" % __version__}) + header) except OSError as e: raise IRCException(e.strerror)