Home > Mobile >  Bash curl command works in terminal, but not with Python os.system()
Bash curl command works in terminal, but not with Python os.system()

Time:05-17

I am trying to make a call to get an api token. If I call curl directly in the terminal I get back a valid token. When I use the os.system() I get returned NULL for the token. Our server at work only lets me run Python2 so I cannot use subprocess.run() as a solution. Here is the call, Any thoughts?

os.system('curl -s http://SeverName:Port/api/tokens?userLogin=Login&password=Password >  /home/debug/logs/Lee/test.txt')

CodePudding user response:

You can use the requests library to do this.

To install:

pip install requests

To use:

import requests

params = {
"userLogin": "Login",
"password": "Password"
}

response = requests.get("http://SeverName:Port/api/tokens", params=params)


with open("/home/debug/logs/Lee/test.txt", "w") as f:
    f.write(response.text)

CodePudding user response:

you can not do it using os.system("...")

os.system() returns the (encoded) process exit value. 0 means success

use python library like requests instead

  • Related