[python]Dynamically store key value pair from API response

This example code demonstrate to store device(key) and id(value) pair in a dictionary.

import requests
from bs4 import BeautifulSoup
USERNAME_SC = 'admin'
PASSWORD_SC = 'p@ssw0rd'
TUFIN_ST_API_BASE = "https://secure_track/securetrack/api/"
TUFIN_SC_API_BASE = "https://secure_change/securechangeworkflow/api/securechange/"
devices = {}
devices_id = []
i = 0
tufin_sc = requests.session()
tufin_sc.verify = False
tufin_sc.auth = requests.auth.HTTPBasicAuth(USERNAME_SC, PASSWORD_SC)
tufin_sc.headers.update({'Content-type': 'application/xml'})


response = tufin_sc.get(TUFIN_SC_API_BASE + "devices")
soup = BeautifulSoup(response.text, 'html.parser')
for id in soup.find_all('id'):
    devices_id.append(id.text)

for name in soup.find_all('name'):
    devices[name.text] = devices_id[i]
    i += 1

print(devices)
Advertisements
This entry was posted in Python, Scripting and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s