【问题标题】:Error while running linux command in Python using subprocess使用子进程在 Python 中运行 linux 命令时出错
【发布时间】:2019-02-28 11:55:06
【问题描述】:

这里的目标是从网站获取密钥或密码。

在 Python 中,我尝试了 os.system() 和 subprocess.call([]) 来运行以下 linux 命令,但两种方法都以错误告终。

os.system("curl -s http://example.com | grep ' "Key" : * ' | cut -f1 -d " " | cut -b5- | rev | cut -b9- | rev") 

在 linux 中正在使用以下,并且工作正常。

PW = 'curl -s http://example.com | grep ' "Key" : * ' | cut -f1 -d " " | cut -b5- | rev | cut -b8- | rev'

我看到的错误是 SyntaxError: invalid syntax,它指向“关键”部分

【问题讨论】:

    标签: linux python-3.x subprocess os.system


    【解决方案1】:

    您可能只需要转义双引号。试试:

    os.system("curl -s http://example.com | grep ' \"Key\" : * ' | cut -f1 -d \" \" | cut -b5- | rev | cut -b9- | rev") 
    

    【讨论】:

    • 你是对的,这就是问题所在。代码现在可以正常运行,但是如何将此代码的输出分配给新变量?我的意思是我尝试做 pass = os.system(....) 但它只是打印密钥但是当我检查变量时它返回 0
    猜你喜欢
    • 2015-12-19
    • 2022-10-19
    • 2021-12-10
    • 2015-10-29
    • 2020-07-25
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    相关资源
    最近更新 更多