[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'):

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


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 )

Facebook photo

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

Connecting to %s