From 8a615bf372cab51d41d41c0feb82d3717c281c3c Mon Sep 17 00:00:00 2001 From: Jacob Welsh Date: Wed, 17 Jun 2015 09:15:21 -0500 Subject: [PATCH] Support Python 3 --- campbx/__init__.py | 2 +- campbx/campbx.py | 19 +++++++++++++------ setup.py | 2 ++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/campbx/__init__.py b/campbx/__init__.py index e2e9eb3..f66a4b9 100644 --- a/campbx/__init__.py +++ b/campbx/__init__.py @@ -1 +1 @@ -from campbx import CampBX \ No newline at end of file +from .campbx import CampBX diff --git a/campbx/campbx.py b/campbx/campbx.py index 052c7f5..8e30c70 100644 --- a/campbx/campbx.py +++ b/campbx/campbx.py @@ -1,9 +1,13 @@ -import urllib2 import base64 import simplejson as json import logging -from urllib import urlencode from functools import partial +try: + import urllib2 + from urllib import urlencode +except ImportError: + import urllib.request as urllib2 + from urllib.parse import urlencode log = logging.getLogger(__name__) log_formatter = logging.Formatter('%(name)s - %(message)s') @@ -14,7 +18,10 @@ log.setLevel(logging.ERROR) opener = urllib2.build_opener() -opener.addheaders = [('User-agent', 'Mozilla/5.0')] +opener.addheaders = [ + ('User-agent', 'Mozilla/5.0'), + ('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8'), +] urllib2.install_opener(opener) @@ -24,7 +31,7 @@ def __new__(cls, func, conf, _repr): return super(EndPointPartial, cls).__new__(cls, func, conf) def __repr__(self): - return unicode('' % self._repr) + return '' % self._repr class CampBX(object): @@ -89,14 +96,14 @@ def _make_request(self, conf, post_params={}): }) # url encode all parameters - data = urlencode(post_params) + data = urlencode(post_params).encode('utf-8') # gimme some bitcoins! try: log.debug('Requesting data from %s' % url) response = urllib2.urlopen(request, data) return json.loads(response.read()) - except urllib2.URLError, e: + except urllib2.URLError as e: log.debug('Full error: %s' % e) if hasattr(e, 'reason'): self.log.error('Could not reach host. Reason: %s' % e.reason) diff --git a/setup.py b/setup.py index f11e288..12b18f7 100644 --- a/setup.py +++ b/setup.py @@ -122,6 +122,8 @@ 'License :: OSI Approved :: MIT License', 'Operating System :: OS Independent', 'Programming Language :: Python', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', 'Topic :: Internet :: WWW/HTTP', 'Topic :: Software Development :: Libraries' ]