Bash curl命令可以在终端中使用,但不能在Python os.system()中使用



我正在尝试拨打电话以获取api令牌。如果我在终端中直接调用curl,我将得到一个有效的令牌。当我使用os.system()时,令牌返回NULL。我们的服务器只允许我运行Python2,所以我不能使用subprocess.run()作为解决方案。电话来了,有什么想法吗?

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

您可以使用请求

安装:

pip install requests

使用:

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)

不能使用os.system("…")

os.system()返回(编码的)进程退出值。0表示成功

使用python库类请求代替

最新更新