Skip to content

Commit

Permalink
Replace urllib2 with requests
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogoAndre committed May 23, 2018
1 parent 1a67434 commit f8288fe
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions napalm_asa/asa.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

from __future__ import unicode_literals

import ssl
import urllib2
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import json
import base64
import re
Expand All @@ -35,8 +35,7 @@
ConnectionException,
CommandErrorException,
)

ssl._create_default_https_context = ssl._create_unverified_context
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


class RespFetcherHttps:
Expand All @@ -61,28 +60,20 @@ def __init__(
def get_resp(self, endpoint="", data=None):
"""Get response from device and returne parsed json."""
full_url = self.base_url + endpoint
req = urllib2.Request(full_url, data, self.headers)
base64string = self.base64_str
req.add_header("Authorization", "Basic %s" % base64string)
f = None
try:
try:
f = urllib2.urlopen(req, data, self.timeout)
status_code = f.getcode()
if (status_code != 200):
raise CommandErrorException("Operation returned an error: {}".format(status_code))
resp = f.read()
json_resp = json.loads(resp)
return json_resp
finally:
if f:
f.close()
except urllib2.URLError, e:
if hasattr(e, 'reason'):
ConnectionException(py23_compat.text_type(e))

elif hasattr(e, 'code'):
ConnectionException(py23_compat.text_type(e))
if data is not None:
f = requests.post(full_url, auth=(self.username, self.password), data=data,
headers=self.headers, timeout=self.timeout, verify=False)
else:
f = requests.get(full_url, auth=(self.username, self.password),
headers=self.headers, timeout=self.timeout, verify=False)
if (f.status_code != 200):
raise CommandErrorException("Operation returned an error: {}".format(f.status_code))

return f.json()
except requests.exceptions.RequestException as e:
raise ConnectionException(py23_compat.text_type(e))


class ASADriver(NetworkDriver):
Expand Down

0 comments on commit f8288fe

Please sign in to comment.