curl → Python Translation

Patterns for translating curl commands to Python requests library.

Basic Mapping

curl Python requests

curl URL

requests.get(url)

-X POST

requests.post(url)

-H "Header: Value"

headers={"Header": "Value"}

-u user:pass

auth=("user", "pass")

-d "data"

data="data"

-d '\{"json": true}'

json={"json": True}

-k (insecure)

verify=False

-o file

open("file", "wb").write(r.content)

Simple GET

curl
curl -ks -u "$USER:$PASS" "https://api.example.com/resource"
Python
import requests

response = requests.get(
    "https://api.example.com/resource",
    auth=(USER, PASS),
    verify=False
)
data = response.json()

POST with JSON

curl
curl -ks -u "$USER:$PASS" \
  -H "Content-Type: application/json" \
  -X POST "https://api.example.com/resource" \
  -d '{"name": "test", "value": 123}'
Python
response = requests.post(
    "https://api.example.com/resource",
    auth=(USER, PASS),
    json={"name": "test", "value": 123},  # auto sets Content-Type
    verify=False
)

Bearer Token

curl
curl -s -H "Authorization: Bearer $TOKEN" "https://api.example.com/resource"
Python
response = requests.get(
    "https://api.example.com/resource",
    headers={"Authorization": f"Bearer {TOKEN}"}
)

ISE ERS Example

curl
curl -ks -u "$ISE_USER:$ISE_PASS" \
  -H "Accept: application/json" \
  "https://$ISE_HOST:9060/ers/config/endpoint"
Python
import os
import requests

ISE_HOST = os.environ["ISE_HOST"]
ISE_USER = os.environ["ISE_USER"]
ISE_PASS = os.environ["ISE_PASS"]

response = requests.get(
    f"https://{ISE_HOST}:9060/ers/config/endpoint",
    auth=(ISE_USER, ISE_PASS),
    headers={"Accept": "application/json"},
    verify=False
)

endpoints = response.json()["SearchResult"]["resources"]
for ep in endpoints:
    print(ep["name"])

Session for Multiple Requests

import requests

# Reuse connection and auth
session = requests.Session()
session.auth = (USER, PASS)
session.verify = False
session.headers.update({"Accept": "application/json"})

# All requests use session config
r1 = session.get(f"https://{HOST}/api/endpoint1")
r2 = session.get(f"https://{HOST}/api/endpoint2")
r3 = session.post(f"https://{HOST}/api/endpoint3", json={"key": "value"})

Error Handling

response = requests.get(url, auth=auth, verify=False)

# Check status
response.raise_for_status()  # Raises exception for 4xx/5xx

# Or manual
if response.status_code == 200:
    data = response.json()
elif response.status_code == 401:
    print("Auth failed")
elif response.status_code == 404:
    print("Not found")
else:
    print(f"Error: {response.status_code}")

mTLS (Client Certificates)

curl
curl -ks --cert client.pem --key client.key --cacert ca.pem \
  "https://secure.example.com/api"
Python
response = requests.get(
    "https://secure.example.com/api",
    cert=("client.pem", "client.key"),
    verify="ca.pem"
)

Full ISE Client Class

import os
import requests
from urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


class ISEClient:
    def __init__(self):
        self.host = os.environ["ISE_HOST"]
        self.session = requests.Session()
        self.session.auth = (os.environ["ISE_USER"], os.environ["ISE_PASS"])
        self.session.verify = False
        self.session.headers.update({"Accept": "application/json"})

    def ers(self, path):
        return self.session.get(f"https://{self.host}:9060/ers/config/{path}").json()

    def openapi(self, path):
        return self.session.get(f"https://{self.host}/api/v1/{path}").json()

    def mnt(self, path):
        return self.session.get(f"https://{self.host}/admin/API/mnt/{path}").json()


# Usage
ise = ISEClient()
endpoints = ise.ers("endpoint")
sessions = ise.mnt("Session/ActiveList")