From beac1b304b18839dd5da7da1648ea4fef0ffa95d Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Thu, 13 Oct 2016 11:43:56 +0200 Subject: [PATCH] [IMP] use urllib2 for transparent proxy support --- pyPostcode/__init__.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/pyPostcode/__init__.py b/pyPostcode/__init__.py index c39ff16..b72817a 100644 --- a/pyPostcode/__init__.py +++ b/pyPostcode/__init__.py @@ -8,7 +8,7 @@ ''' -import httplib +import urllib2 import json import logging @@ -33,9 +33,9 @@ def __init__(self, api_key, api_version=(2, 0, 0)): self.api_key = api_key self.api_version = api_version if api_version >= (2, 0, 0): - self.url = 'postcode-api.apiwise.nl' + self.url = 'https://postcode-api.apiwise.nl' else: - self.url = 'api.postcodeapi.nu' + self.url = 'http://api.postcodeapi.nu' def handleresponseerror(self, status): if status == 401: @@ -61,21 +61,14 @@ def request(self, path=None): "X-Api-Key": self.api_key, } - if self.api_version >= (2, 0, 0): - conn = httplib.HTTPSConnection(self.url) - else: - conn = httplib.HTTPConnection(self.url) - '''Only GET is supported by the API at this time''' - conn.request('GET', path, None, headers) - - result = conn.getresponse() + result = urllib2.urlopen(urllib2.Request( + self.url + path, headers=headers, + )) - if result.status is not 200: - conn.close() - self.handleresponseerror(result.status) + if result.getcode() is not 200: + self.handleresponseerror(result.getcode()) resultdata = result.read() - conn.close() jsondata = json.loads(resultdata)