BirdyNet/addons/0.pageDownloader.py

26 lines
589 B
Python
Raw Normal View History

2021-03-18 15:22:56 +00:00
global urllib
import urllib.request
2021-03-20 15:41:10 +00:00
global html
import html
2021-03-18 15:22:56 +00:00
global downloadPage
def downloadPage(url,headers = False):
if not headers: headers = {}
2021-03-20 15:41:10 +00:00
response = {
"url": url,
"body": None,
"headers": []
}
2021-03-18 15:22:56 +00:00
request = urllib.request.Request(url,headers=headers)
2021-03-20 15:41:10 +00:00
requestHandler = None
2021-03-18 15:22:56 +00:00
try:
2021-03-20 15:41:10 +00:00
requestHandler = urllib.request.urlopen(request)
response["headers"] = requestHandler.getheaders()
response["body"] = requestHandler.read()
2021-03-18 15:22:56 +00:00
except urllib.error.HTTPError as e:
2021-03-20 15:41:10 +00:00
response["body"] = e.read()
except Exception as e:
response["body"] = html.escape(e)
2021-03-18 15:22:56 +00:00
2021-03-20 15:41:10 +00:00
return response