From 5a065701c769c94228b590546100483d26a65ded Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 14 Nov 2017 23:18:55 +0400 Subject: [PATCH] misc: switch http support to requests library (#19437) --- debian/control | 1 + wcs/qommon/misc.py | 41 ++++++++++++----------------------------- 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/debian/control b/debian/control index cd954f2d..207a294c 100644 --- a/debian/control +++ b/debian/control @@ -16,6 +16,7 @@ Depends: ${misc:Depends}, ${python:Depends}, graphviz, python-feedparser, python-imaging, + python-requests, python-vobject, uwsgi, uwsgi-plugin-python diff --git a/wcs/qommon/misc.py b/wcs/qommon/misc.py index 777464d5..e04e195d 100644 --- a/wcs/qommon/misc.py +++ b/wcs/qommon/misc.py @@ -31,6 +31,8 @@ import tempfile import unicodedata import hashlib +import requests + try: from PIL import Image except ImportError: @@ -276,6 +278,7 @@ def _http_request(url, method='GET', body=None, headers={}, cert_file=None, time else: hostname, query = urllib.splithost(url[6:]) + auth = None if '@' in hostname: authenticator, hostname = hostname.split('@') if ':' in authenticator: @@ -283,39 +286,19 @@ def _http_request(url, method='GET', body=None, headers={}, cert_file=None, time else: username = authenticator password = '' - headers['Authorization'] = 'Basic %s' % base64.b64encode('%s:%s' % (username, password)) - - connection_kwargs = {'host': hostname} - if timeout: - connection_kwargs['timeout'] = timeout - if cert_file: - connection_kwargs['cert_file'] = cert_file - if url.startswith('http://'): - conn = httplib.HTTPConnection(**connection_kwargs) - else: # https - conn = httplib.HTTPSConnection(**connection_kwargs) - query = query.replace('&', '&') + auth = (username, password) try: - conn.request(method, query, body, headers) - response = conn.getresponse() - except socket.gaierror, (err, msg): - conn.close() - raise ConnectionError('error while connecting to %s (%s)' % (hostname, msg)) - except socket.timeout, err: - conn.close() + response = requests.request(method, url, headers=headers, data=body, + timeout=timeout, cert=cert_file) + except requests.Timeout: raise ConnectionError('connection timed out while fetching the page') - except socket.error, err: - conn.close() - raise ConnectionError('error while fetching the page (%s)' % err) - except ssl.CertificateError, err: - conn.close() - raise ConnectionError('certificate error when connecting (%s)' % err) + except requests.RequestException as err: + raise ConnectionError('error in http request to to %s (%s)' % (hostname, err)) else: - data = response.read() - status = response.status - auth_header = response.getheader('WWW-Authenticate') - conn.close() + data = response.content + status = response.status_code + auth_header = response.headers.get('WWW-Authenticate') return response, status, data, auth_header -- 2.15.0