apache2上的os.process加载到usr / sbin /中,并且无法识别命令模块

问题描述

我正在尝试在apache2服务器上托管的flask应用程序中运行子过程命令。

当我运行子流程时(确认的代码在apache2之外可以运行)

@app.route("/ScanResult")
def scan_result():
    process = subprocess.run('face_recognition ./kNown_faces/ ./unkNown_faces/',shell=True,check=True,stdout=subprocess.PIPE,universal_newlines=True)
    output = process.stdout
    flash(output)
    return render_template("scanresult.html")

我收到一个错误

Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
 Command line: '/usr/sbin/apache2'

/bin/sh: 1: face_recognition: not found
ERROR:webApp:Exception on /ScanResult [GET],referer: http://faces-app.duckdns.org:2020/Scan
Traceback (most recent call last):,referer: http://faces-app.duckdns.org:2020/Scan
   File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py",line 2447,in wsgi_app,referer: http://faces-app.duckdns.org:2020/Scan
    response = self.full_dispatch_request(),referer: http://faces-app.duckdns.org:2020/Scan
  File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py",line 1952,in full_dispatch_request,referer: http://faces-app.duckdns.org:2020/Scan
    rv = self.handle_user_exception(e),referer: http://faces-app.duckdns.org:2020/Scan
 File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py",line 1821,in handle_user_exception,referer: http://faces-app.duckdns.org:2020/Scan
    reraise(exc_type,exc_value,tb),referer: http://faces-app.duckdns.org:2020/Scan
  File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/_compat.py",line 39,in reraise,referer: http://faces-app.duckdns.org:2020/Scan
    raise value,line 1950,referer: http://faces-app.duckdns.org:2020/Scan
    rv = self.dispatch_request(),line 1936,in dispatch_request,referer: http://faces-app.duckdns.org:2020/Scan
    return self.view_functions[rule.endpoint](**req.view_args),referer: http://faces-app.duckdns.org:2020/Scan
   File "/var/www/webApp/webApp/__init__.py",line 368,in scan_result,referer: http://faces-app.duckdns.org:2020/Scan
   stdout=subprocess.PIPE,universal_newlines=True),referer: http://faces-app.duckdns.org:2020/Scan
 File "/usr/lib/python3.6/subprocess.py",line 438,in run,referer: http://faces-app.duckdns.org:2020/Scan
 output=stdout,stderr=stderr),referer: http://faces-app.duckdns.org:2020/Scan
 subprocess.CalledProcessError: Command 'face_recognition .../kNown_faces/ .../unkNown_faces/' returned non-zero exit status 127.,referer: http://faces-app.duckdns.org:2020/Scan

python模块安装在venv中,并且烧瓶应用程序按预期工作,除了该子进程。

如何将命令终端启动到venv中,而不是在usr / sbin / apache2或/ bin / sh中启动??

我确实激活了静脉。

我试图将env传递给子流程语句,但没有成功。

 process = subprocess.run('face_recognition ./kNown_faces/ ./unkNown_faces/',universal_newlines=True,env={"PATH":"/home/thomas/venv/apache_flask_venv/bin/"},)

这是来自此答案的。 add env to subprocess

将路径添加到模块位置后,更改了错误

[Tue Sep 01 19:02:40.459001 2020] [mpm_event:notice] [pid 6552:tid 140697108794304] AH00491: caught SIGTERM,shutting down
[Tue Sep 01 19:02:40.622309 2020] [mpm_event:notice] [pid 6725:tid 140301125479360] AH00489: Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Tue Sep 01 19:02:40.622638 2020] [core:notice] [pid 6725:tid 140301125479360] AH00094: Command line: '/usr/sbin/apache2'
Dlib was compiled to use SSE41 instructions,but these aren't available on your machine.
Dlib was compiled to use SSE41 instructions,but these aren't available on your machine.
Traceback (most recent call last):
  File "/home/thomas/venv/apache_flask_venv/bin/face_recognition",line 11,in <module>
    sys.exit(main())
  File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/click/core.py",line 829,in __call__
    return self.main(*args,**kwargs)
  File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/click/core.py",line 760,in main
    _verify_python3_env()
  File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/click/_unicodefun.py",line 130,in _verify_python3_env
    " mitigation steps.{}".format(extra)
RuntimeError: Click will abort further execution because Python 3 was configured to use ASCII as encoding for the environment. Consult https://click.palletsprojects.com/python3/ for mitigation steps.

This system supports the C.UTF-8 locale which is recommended. You might be able to resolve your issue by exporting the following environment variables:

    export LC_ALL=C.UTF-8
    export LANG=C.UTF-8
[Tue Sep 01 19:02:54.732537 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789] ERROR:webApp:Exception on /ScanResult [GET],referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732679 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789] Traceback (most recent call last):,referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732706 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]   File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py",referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732739 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]     response = self.full_dispatch_request(),referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732762 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]   File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py",referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732786 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]     rv = self.handle_user_exception(e),referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732809 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]   File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py",referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732832 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]     reraise(exc_type,referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732854 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]   File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/_compat.py",referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732877 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]     raise value,referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732899 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]   File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py",referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.732993 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]     rv = self.dispatch_request(),referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.733019 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]   File "/home/thomas/venv/apache_flask_venv/lib/python3.6/site-packages/flask/app.py",referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.733043 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]     return self.view_functions[rule.endpoint](**req.view_args),referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.733066 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]   File "/var/www/webApp/webApp/__init__.py",line 369,referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.733089 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]     stdout=subprocess.PIPE,),referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.733111 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]   File "/usr/lib/python3.6/subprocess.py",referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.733133 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789]     output=stdout,referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.733164 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789] subprocess.CalledProcessError: Command '/home/thomas/venv/apache_flask_venv/bin/face_recognition ../kNown_faces/ ../unkNown_faces/' returned non-zero exit status 1.,referer: http://faces-app.duckdns.org:2020/Scan
[Tue Sep 01 19:02:54.733206 2020] [wsgi:error] [pid 6729:tid 140300676757248] [client 185.194.96.195:57789],referer: http://faces-app.duckdns.org:2020/Scan

我开始审查似乎与点击模块有关的错误unix bytes not unicode issue

如果我可以在终端中运行它,那我肯定可以通过我的烧瓶应用程序运行它,不是吗?

(apache_flask_venv) thomas@apacheflask:/var/www/webApp/webApp$ face_recognition ./faces/ ./scan/
Dlib was compiled to use SSE41 instructions,but these aren't available on your machine.
./scan/singleperson.jpg,singleperson

我尝试了建议的解决方案:

export LC_ALL = en_US.utf-8
export LANG = en_US.utf-8

插入.py文件的开头。导致错误

[Tue Sep 01 19:24:17.852576 2020] [wsgi:error] [pid 7472:tid 140654836156160] [client 185.194.96.195:59238]     export LC_ALL = en_US.utf-8
[Tue Sep 01 19:24:17.852667 2020] [wsgi:error] [pid 7472:tid 140654836156160] [client 185.194.96.195:59238]                 ^
[Tue Sep 01 19:24:17.852729 2020] [wsgi:error] [pid 7472:tid 140654836156160] [client 185.194.96.195:59238] SyntaxError: invalid Syntax

如何修改子流程以匹配建议的解决方案:

Instead you need to do this:

input = 'Input here'
in_stream = io.BytesIO(input.encode('utf-8'))
sys.stdin = io.TextIOWrapper(in_stream,encoding='utf-8')
out_stream = io.BytesIO()
sys.stdout = io.TextIOWrapper(out_stream,encoding='utf-8')
This causes problems if the terminal is incorrectly set and Python does not figure out the encoding. In that case,the Unicode string will contain error bytes encoded as surrogate escapes.

我需要更改语言环境吗?

我的语言环境:

(apache_flask_venv) thomas@apacheflask:/var/www/webApp/webApp$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

将语言环境修改为建议的语言环境,而未对错误进行任何更改。

(apache_flask_venv) thomas@apacheflask:/var/www/webApp/webApp$ locale
LANG=C.UTF-8
LANGUAGE=
LC_CTYPE="C.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_PAPER="C.UTF-8"
LC_NAME="C.UTF-8"
LC_ADDRESS="C.UTF-8"
LC_TELEPHONE="C.UTF-8"
LC_MEASUREMENT="C.UTF-8"
LC_IDENTIFICATION="C.UTF-8"
LC_ALL=C.UTF-8

解决方法

到目前为止,我发现的唯一解决方案是利用python2而不是python3-这需要将必需的dlib和face_recognition安装到python2环境中,然后在命令前调用python2.6。

相反,我创建了一个复制命令行工具功能的方法。

class TestSuite(LiveServerTestCase):

    @classmethod
    def setUp(self):
        self.driver = webdriver.Chrome()
        
        # Login User
        self.driver.get(self.live_server_url + '/somesite/login/')
        self.driver.find_element(By.ID,"username").click()
        self.driver.find_element(By.ID,"username").send_keys("foo")
        self.driver.find_element(By.ID,"password").click()
        self.driver.find_element(By.ID,"password").send_keys("bar")
        self.driver.find_element(By.ID,"login_button").click()
        # Redirects to home page
    
    @classmethod
    def tearDown(self):
        self.driver.quit()

    def test_foo(self):
        # Do some clicking with already logged in user from home page

    def test_bar(self):
        # Do some clicking with already logged in user from home page

如果任何人都具有解决与语言环境变量相关的问题的完整解决方案,请发布。除了在python2环境中重新安装和构建外,修改LANG等的建议解决方案也不起作用。