Skip to content

Commit c0f9a91

Browse files
committed
httptools: Remove need for urllib2, make python 2/3 compatible
1 parent 7c84174 commit c0f9a91

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

qencode/_compat.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,11 @@
66

77
if PY2:
88
string_types = (str, unicode)
9+
from urllib import urlencode
10+
from urllib2 import urlopen, Request, HTTPError, URLError
11+
from urlparse import urljoin
912
else:
1013
string_types = (str,)
14+
from urllib.parse import urlencode, urljoin
15+
from urllib.request import urlopen, Request
16+
from urllib.error import HTTPError, URLError

qencode/httptools.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
2-
import urllib
3-
import urllib2
4-
from urlparse import urljoin
2+
3+
from ._compat import HTTPError, Request, URLError, urlencode, urljoin, urlopen
54

65

76
class Http(object):
@@ -14,18 +13,18 @@ def _call_server(self, url, post_data):
1413
if not url:
1514
response = dict(error=True, message='AttributeError: Bad URL')
1615
return json.dumps(response)
17-
data = urllib.urlencode(post_data)
18-
request = urllib2.Request(url, data)
16+
data = urlencode(post_data)
17+
request = Request(url, data)
1918
try:
20-
res = urllib2.urlopen(request)
21-
except urllib2.HTTPError as e:
19+
res = urlopen(request)
20+
except HTTPError as e:
2221
headers = e.headers if self._debug else ''
2322
response = dict(
2423
error=True,
2524
message='HTTPError: {0} {1} {2}'.format(e.code, e.reason, headers),
2625
)
2726
response = json.dumps(response)
28-
except urllib2.URLError as e:
27+
except URLError as e:
2928
response = dict(error=True, message='URLError: {0}'.format(e.reason))
3029
response = json.dumps(response)
3130
else:

0 commit comments

Comments
 (0)