0

I know you can shorten down the length of this python api call to hue using a variable but I cant figure out the format or the proper term to find the answer in stackoverflow. I would like to learn how to hide the auth token to practice better security and add a variable for multiple lights that i may add in the future. I believe i would need multiple variables within the URL. Please any help would be greatly appreciated.

import requests

url = "http://192.168.98.233/api/Cjk7782cABRgUggxUTlt8DwwnK516ilhMHzHwFlq/lights/{}/state".format(1)
url2= "http://192.168.98.233/api/Cjk7782cABRgUggxUTlt8DwwnK516ilhMHzHwFlq/lights/{}/state".format(2)
url3= "http://192.168.98.233/api/Cjk7782cABRgUggxUTlt8DwwnK516ilhMHzHwFlq/lights/{}/state".format(3)

payload = " {\"on\":false}"
headers = {
    'content-type': "application/json",
    'cache-control': "no-cache"
    }

r = requests.put(url, data=payload, headers=headers)
r2 = requests.put(url2, data=payload, headers=headers)
r3 = requests.put(url3, data=payload, headers=headers)
print(r.text)
print(r2.text)
print(r3.text)
Matthew Morcaldi
  • 466
  • 1
  • 5
  • 20

1 Answers1

1

Like this :

import requests

payload = " {\"on\":false}"
headers = {
    'content-type': "application/json",
    'cache-control': "no-cache"
}

for i in range(1, 4) : 
    url = "http://192.168.98.233/api/Cjk7782cABRgUggxUTlt8DwwnK516ilhMHzHwFlq/lights/{}/state".format(i)
    r = requests.put(url, data=payload, headers=headers)
    print(r.text)
t.m.adam
  • 15,106
  • 3
  • 32
  • 52