Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix content type #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pycsco/nxos/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def show(self, command, fmat='xml', text=False):
self.sw1.set_out_format(fmat)
self.sw1.set_cmd(command)

data = self.sw1.send_req()
data = self.sw1.send_req(fmat=fmat)

if fmat == 'xml':
data_dict = xmltodict.parse(data[1])
Expand All @@ -126,7 +126,7 @@ def config(self, command, fmat='xml'):
self.sw1.set_out_format(fmat)
self.sw1.set_cmd(command)

data = self.sw1.send_req()
data = self.sw1.send_req(fmat=fmat)
# return self.sw1.send_req
if fmat == 'xml':
data_dict = xmltodict.parse(data[1])
Expand Down
22 changes: 20 additions & 2 deletions pycsco/nxos/nxapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
print '***************************'


FORMAT_TO_CONTENT_TYPE = {
"xml": "text/xml",
"json": "application/json"
}


class HTTPSConnection(HTTPConnection):

'''This class allows communication via SSL.'''
Expand Down Expand Up @@ -118,13 +124,15 @@ def __init__(
username='admin',
password='insieme',
url='http://172.21.128.227/ins',
content_type=None
):

self.username = username
self.password = password
self.url = url
self.base64_str = base64.encodestring('%s:%s' % (username,
password)).replace('\n', '')
self.content_type = content_type

def get_resp(
self,
Expand All @@ -136,6 +144,8 @@ def get_resp(
req = urllib2.Request(self.url, req_str)
req.add_header('Authorization', 'Basic %s' % self.base64_str)
req.add_header('Cookie', '%s' % cookie)
if self.content_type is not None:
req.add_header('Content-Type', self.content_type)
try:
with contextlib.closing(urllib2.urlopen(req,
timeout=timeout)) as resp:
Expand Down Expand Up @@ -282,6 +292,14 @@ def req_to_string(self):
req_msg += '</ins_api>\n'
return req_msg

def send_req(self):
req = RespFetcher(self.username, self.password, self.target_url)
def send_req(self, fmat="xml"):
if fmat is not None:
content_type = FORMAT_TO_CONTENT_TYPE.get(fmat)
else:
content_type = None

req = RespFetcher(
self.username, self.password, self.target_url,
content_type=content_type
)
return req.get_resp(self.req_to_string(), self.cookie, self.timeout)