I am trying to call environment variables from Python script but having issues figuring out syntax:
Here is my script:
import requests
import os
user = os.environ.get('api_user')
password = os.environ.get('api_pass')
headers = {
'accept': 'application/xml',
}
response = requests.post('https://mysite.mydomain.com/bla/commands/command/command/id/54', headers=headers, auth=(os.environ['user',"password"]))
Any suggestions how to call variables from OS environment?
It works if I use just curl
curl -X GET "https://mysite.mydomain.com/bla/commands/command/command/id/54" \
-H "user: ${user}" \
-H "password: ${password}" \
But trying to figure this out in Python which I have no experience.
os.environ
is a like dictionary; you can't index multiple keys like you have in your example: (os.environ['user',"password"])
You need to access dict keys individually like at the top part of your code:
user = os.environ.get('api_user')
password = os.environ.get('api_pass')
Then if auth requires a dict of user and password, you can pass:
response = requests.post('https://mysite.mydomain.com/bla/commands/command/command/id/54', headers=headers, auth={'user': user, 'password': password})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.