|
13 | 13 | try:
|
14 | 14 | IsADirectoryError # pylint: disable=used-before-assignment
|
15 | 15 | except NameError:
|
16 |
| - IsADirectoryError = OSError |
| 16 | + IsADirectoryError = IOError |
17 | 17 |
|
18 | 18 | try:
|
19 | 19 | from urllib.parse import urlencode
|
20 | 20 | from urllib.request import Request, urlopen
|
21 | 21 | except ImportError:
|
22 | 22 | from urllib import urlencode
|
23 | 23 |
|
24 |
| - from urllib2 import urlopen |
| 24 | + from urllib2 import Request, urlopen |
25 | 25 |
|
26 | 26 |
|
27 | 27 | VOMS_FQANS_OID = b"1.3.6.1.4.1.8005.100.100.4"
|
@@ -141,12 +141,22 @@ def executeRequest(self, raw_data, insecure=False):
|
141 | 141 | ctx.check_hostname = False
|
142 | 142 | ctx.verify_mode = ssl.CERT_NONE
|
143 | 143 |
|
144 |
| - with urlopen(request, context=ctx) as res: |
145 |
| - response_data = res.read().decode("utf-8") # Decode response bytes |
| 144 | + if sys.version_info[0] >= 3: |
| 145 | + # Python 3 code |
| 146 | + with urlopen(request, context=ctx) as res: |
| 147 | + response_data = res.read().decode("utf-8") # Decode response bytes |
| 148 | + else: |
| 149 | + # Python 2 code |
| 150 | + res = urlopen(request, context=ctx) |
146 | 151 | try:
|
147 |
| - return json.loads(response_data) # Parse JSON response |
148 |
| - except json.JSONDecodeError: |
149 |
| - raise Exception("Invalid JSON response: %s" % response_data) |
| 152 | + response_data = res.read().decode("utf-8") # Decode response bytes |
| 153 | + finally: |
| 154 | + res.close() |
| 155 | + |
| 156 | + try: |
| 157 | + return json.loads(response_data) # Parse JSON response |
| 158 | + except ValueError: # In Python 2, json.JSONDecodeError is a subclass of ValueError |
| 159 | + raise Exception("Invalid JSON response: %s" % response_data) |
150 | 160 |
|
151 | 161 |
|
152 | 162 | class TokenBasedRequest(BaseRequest):
|
|
0 commit comments