From 235aad37cfbdb8333f920ccc66e9237340b3b0d2 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 21 Jun 2015 12:44:26 +0200 Subject: [PATCH 1/2] switched from http.client to urllib for fetching external resources to respect http_proxy environment variables --- lib/tools.py | 27 ++++++--------------------- plugins/prowl/__init__.py | 18 ++++++++++-------- 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/lib/tools.py b/lib/tools.py index f617cac6..5006bcf4 100755 --- a/lib/tools.py +++ b/lib/tools.py @@ -26,6 +26,7 @@ import math import subprocess import time +import urllib.request logger = logging.getLogger('') @@ -56,32 +57,16 @@ def dt2ts(self, dt): return time.mktime(dt.timetuple()) def fetch_url(self, url, username=None, password=None, timeout=2): - headers = {'Accept': 'text/plain'} - plain = True - if url.startswith('https'): - plain = False - lurl = url.split('/') - host = lurl[2] - purl = '/' + '/'.join(lurl[3:]) - if plain: - conn = http.client.HTTPConnection(host, timeout=timeout) - else: - conn = http.client.HTTPSConnection(host, timeout=timeout) - if username and password: - headers['Authorization'] = ('Basic '.encode() + base64.b64encode((username + ':' + password).encode())) - try: - conn.request("GET", purl, headers=headers) - except Exception as e: - logger.warning("Problem fetching {0}: {1}".format(url, e)) - conn.close() - return False - resp = conn.getresponse() + req = urllib.request.Request(url) + req.add_header = ('Accept','text/plain') + resp = urllib.request.urlopen(req, timeout=5) + if resp.status == 200: content = resp.read() else: logger.warning("Problem fetching {0}: {1} {2}".format(url, resp.status, resp.reason)) content = False - conn.close() + return content def rel2abs(self, t, rf): diff --git a/plugins/prowl/__init__.py b/plugins/prowl/__init__.py index 5c7249d0..4f61a899 100755 --- a/plugins/prowl/__init__.py +++ b/plugins/prowl/__init__.py @@ -22,14 +22,12 @@ import logging import urllib.parse import http.client +import urllib.request logger = logging.getLogger('Prowl') class Prowl(): - _host = 'api.prowlapp.com' - _api = '/publicapi/add' - def __init__(self, smarthome, apikey=None): self._apikey = apikey self._sh = smarthome @@ -42,7 +40,6 @@ def stop(self): def __call__(self, event='', description='', priority=None, url=None, apikey=None, application='SmartHome'): data = {} - headers = {'User-Agent': "SmartHome.py", 'Content-Type': "application/x-www-form-urlencoded"} data['event'] = event[:1024].encode() data['description'] = description[:10000].encode() data['application'] = application[:256].encode() @@ -55,10 +52,15 @@ def __call__(self, event='', description='', priority=None, url=None, apikey=Non if url: data['url'] = url[:512] try: - conn = http.client.HTTPSConnection(self._host, timeout=4) - conn.request("POST", self._api, urllib.parse.urlencode(data), headers) - resp = conn.getresponse() - conn.close() + apiurl='https://api.prowlapp.com/publicapi/add' + data = urllib.parse.urlencode(data) + data = data.encode('utf-8') + req = urllib.request.Request(apiurl, data) + req.add_header = ('User-Agent','SmartHome.py') + req.add_header = ('Content-Type','application/x-www-form-urlencoded;charset=utf-8') + + logger.debug("lala"); + resp = urllib.request.urlopen(req,timeout=5) if resp.status != 200: raise Exception("{} {}".format(resp.status, resp.reason)) except Exception as e: From d8a91c758a877836d9e8a1378487e0968a973ede Mon Sep 17 00:00:00 2001 From: Florian Meister Date: Sun, 21 Jun 2015 21:17:59 +0200 Subject: [PATCH 2/2] switched from http.client to urllib for fetching external resources to respect http_proxy environment variables: - fixed timeout, BasicAuth and Debug-Handling --- lib/tools.py | 5 ++++- plugins/prowl/__init__.py | 3 +-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/tools.py b/lib/tools.py index 5006bcf4..de653115 100755 --- a/lib/tools.py +++ b/lib/tools.py @@ -59,7 +59,10 @@ def dt2ts(self, dt): def fetch_url(self, url, username=None, password=None, timeout=2): req = urllib.request.Request(url) req.add_header = ('Accept','text/plain') - resp = urllib.request.urlopen(req, timeout=5) + if username and password: + req.add_header = ('Authorization', 'Basic '.encode() + base64.b64encode((username + ':' + password).encode())) + + resp = urllib.request.urlopen(req, timeout=timeout) if resp.status == 200: content = resp.read() diff --git a/plugins/prowl/__init__.py b/plugins/prowl/__init__.py index 4f61a899..d8b89230 100755 --- a/plugins/prowl/__init__.py +++ b/plugins/prowl/__init__.py @@ -59,8 +59,7 @@ def __call__(self, event='', description='', priority=None, url=None, apikey=Non req.add_header = ('User-Agent','SmartHome.py') req.add_header = ('Content-Type','application/x-www-form-urlencoded;charset=utf-8') - logger.debug("lala"); - resp = urllib.request.urlopen(req,timeout=5) + resp = urllib.request.urlopen(req,timeout=4) if resp.status != 200: raise Exception("{} {}".format(resp.status, resp.reason)) except Exception as e: