python base64 requests jq and terraform

base64 user auth in requests use the base64 for the first header and then reuse the bearer token that you get back. import requests import json from requests.auth import HTTPBasicAuth headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } base_url = 'https://www.something.com' token_url = "/tauth/1.0/token/" url=base_url + token_url response = requests.post(url, headers=headers, auth=HTTPBasicAuth(USER, PASSWORD)) data = response.json() #reusing that bearer token from the repsonse headers = { 'Authorization': f"Bearer {data['token']}", 'Content-Type': 'application/json', 'Accept': 'application/json' } url="/something/" response=requests....

2023-05-23 · 1 min · 140 words · Mike Fettis

python nested dictionary access

Getting dictionaries back from a http endpoint usually results in a mess of objects. Dealing with this in python can be a challenge of ValueError exceptions. so what is the easiest way to handle this? a nested get. if the object looks like message={"detail":{"operation":"create"}} then you can access it like this message.get('detail', {}).get('operation') then if you access something that doesn’t exist you will get a nice None back >>> print(message.get('detail', {})....

2023-04-27 · 1 min · 78 words · Mike Fettis

python repl

Using repl to debug python. https://jvns.ca/blog/2021/09/16/debugging-in-a-repl-is-fun/ what’s a REPL? REPL stands for “read eval print loop”. A REPL is a program that: reads some input from you like print(f"2 + 2 = {2+2}") (read) evaluates the input (eval) print out the result (print) and then goes back to step 1 (loop) A couple years ago I used pry in ruby/chef a bunch and then I guess I forgot about it. Now this works for python too and it is WAY better than my print statement hell that I usually write....

2021-09-16 · 1 min · 151 words · Mike Fettis

splunk-hec-python

Splunk HTTP collector python script Hey the http collector is awesome so let use it in python to send things to stuff… This is going to just take a dictionary as event data and then pass that to the http collector using only the request library . You will need to have a collector enabled for splunk and a token generated. #!/usr/bin/env python import time import requests import urllib3 ##turns off the warning that is generated below because using self signed ssl cert urllib3....

2018-10-25 · 2 min · 255 words · Mike Fettis

pythonic list find

finding an item in a list python can do a lot of smart things, lets use the python way of finding items in a list. #python 3 tags=[ { "Key": "CreationDate", "Value": "201904011158" }, { "Key": "Name", "Value": "mymagicserver" }, { "Key": "aws:autoscaling:groupName", "Value": "my-rad-asg" }, { "Key": "Environment", "Value": "cloudy" }, { "Key": "name", "Value": "john" }, { "Key": "Department", "Value": "sporting goods" }, { "Key": "Auto-Off", "Value": "no" } ] data = list(filter(lambda x: x["Key"] == "aws:autoscaling:groupName" , tags)) print(data[0]["Value"]) ##brute force way: def bruteforce(mylist) for item in mylist: if item["Key"] == "aws:autoscaling:groupName": return item["Value"] print(bruteforce(tags))

1 min · 98 words · Mike Fettis