[tools/web] Add header param to getContentUrl()

Add the possibility to specify headers when querying websites.
This commit is contained in:
Max 2015-10-08 02:47:42 +01:00
parent 684806baaf
commit c55e66dd70

View File

@ -60,7 +60,7 @@ def getPassword(url):
# Get real pages # 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 """Return page content corresponding to URL or None if any error occurs
Arguments: Arguments:
@ -97,19 +97,24 @@ def getURLContent(url, body=None, timeout=7):
else: else:
raise IRCException("Invalid URL") 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 import socket
try: try:
from nemubot import __version__
if o.query != '': if o.query != '':
conn.request("GET" if body is None else "POST", conn.request("GET" if body is None else "POST",
o.path + "?" + o.query, o.path + "?" + o.query,
body, body,
{"User-agent": "Nemubot v%s" % __version__}) header)
else: else:
conn.request("GET" if body is None else "POST", conn.request("GET" if body is None else "POST",
o.path, o.path,
body, body,
{"User-agent": "Nemubot v%s" % __version__}) header)
except OSError as e: except OSError as e:
raise IRCException(e.strerror) raise IRCException(e.strerror)