【问题标题】:uWSGI + Python subprocess does not execute shell commanduWSGI + Python子进程不执行shell命令
【发布时间】:2020-05-23 09:07:21
【问题描述】:

uwsgi + Python 子进程

大家好,

我正在尝试使用 Python 子进程模块在 shell 中运行一个简单的命令,一切正常,直到我输入 uwsgi 在顶部。我也使用flask作为web框架。

这里是非常简化的部分代码

if request.method == 'POST':  

            testquery = subprocess.run( "ifconfig", shell=True,stdout=subprocess.PIPE,universal_newlines=True )
            whoisresults=whoisquery.stdout
            print(whoisresults)

我收到以下错误:/bin/sh: 1: ifconfig: not found

我将“ifconfig”替换为 Python 虚拟 evn 运行的完整路径。

testquery = subprocess.run( "/home/net/netools/netoolsenv/bin ifconfig", shell=True,stdout=subprocess.PIPE,universal_newlines=True )
            whoisresults=whoisquery.stdout
            print(whoisresults)

但还是不行,只是错误不同——/bin/sh: 1: /home/net/netools/netoolsenv/bin: Permission denied

谁能建议我应该朝哪个方向挖掘?我是初学者。

【问题讨论】:

  • Permission Denied 表明您对该文件没有权限。另外“/home/net/netools/netoolsenv/bin ifconfig 路径似乎无效”,我认为您应该删除空格
  • 我认为这与 uWSGI 无关。另外,为什么shell=True?

标签: python subprocess


【解决方案1】:

我收到以下错误:/bin/sh: 1: ifconfig: not found

uWSGI 守护进程通常作为另一个用户运行,并且这个用户没有PATH 设置。使用ifconfig 的完整绝对路径。

要查找工具的完整路径,请使用 which,例如在你的终端上运行这个命令:

$ which ifconfig
/usr/bin/ifconfig

并在 Python 脚本中使用指向 ifconfig 的完整路径。

testquery = subprocess.run( "/usr/bin/ifconfig", shell=True,stdout=subprocess.PIPE,universal_newlines=True )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 2021-09-08
    • 2020-01-20
    • 2017-11-18
    相关资源
    最近更新 更多