简体   繁体   English

使用来自 python 的附加参数运行 shell 脚本并将输出作为 python 变量(API 身份验证)

[英]Run shell script with additional parameters from python and get output as python variable (API authentication)

Could someone help me with the following:有人可以帮我解决以下问题:

I have this line that I normally run in terminal:我有这条线,我通常在终端中运行:

 path/to/mysh/script.sh PROD generate-ticket username

and it gives me the following output in terminal:它在终端中给了我以下输出:

{"user":{"Userid":123,"firstName":"Joe","SecondName":"Doe","loginName":"jdoe"},"accessToken":"1235df6543cggf432sa24"}

I need the 'accessToken' value assigned to python variable.我需要分配给 python 变量的“accessToken”值。

My code:我的代码:

import subprocess
import json 

username ='jdoe'

def get_Access_Token(username):
    s = json.loads(subprocess.check_output(['path/to/mysh/script.sh', 'PROD', 'generate-ticket', username]))


print (s['accessToken'])

but it gives me the following error when trying to print my variable:但是在尝试打印我的变量时它给了我以下错误:

NameError: name 's' is not defined

Thanks in advance!提前致谢!

Return the value from the function.从函数返回值。

Ex:前任:

import subprocess
import json 

username ='jdoe'

def get_Access_Token(username):
    return json.loads(subprocess.check_output(['path/to/mysh/script.sh', 'PROD', 'generate-ticket', username]))['accessToken']

print (get_Access_Token(username))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM