android - Python 系统调用找不到文件,在 Apache 服务器上运行出错

标签 android python django linux apache

我正在尝试从我的 Python 类运行一个 os.system() 调用来获取我存储在文件中的文件。我正在从 Android 应用程序连接到服务器并在 Django 中运行一个方法来运行系统调用。服务器正在运行带有 mod_wsgi 的 Apache 以部署 Django。这是 Django 方法:

def post_try(request):
    os.chdir("/usr/local/src")
    response = os.system("source sourceme")
    return HttpResponse(response)

就语法而言,代码工作正常,所有必要的导入都已完成,但是我一直收到 256 错误代码,而不是预期的 0。我检查了 Apache 日志中的错误,这就是我得到的:

[Sun Aug 18 19:43:23 2013] [notice] caught SIGTERM, shutting down
[Sun Aug 18 19:43:24 2013] [warn] mod_wsgi: Compiled for Python/2.7.2+.
[Sun Aug 18 19:43:24 2013] [warn] mod_wsgi: Runtime using Python/2.7.3.
[Sun Aug 18 19:43:24 2013] [notice] Apache/2.2.22 (Ubuntu) mod_wsgi/3.3 Python/2.7.3 configured -- resuming normal operations
[Sun Aug 18 19:46:04 2013] [notice] caught SIGTERM, shutting down
[Sun Aug 18 19:46:05 2013] [warn] mod_wsgi: Compiled for Python/2.7.2+.
[Sun Aug 18 19:46:05 2013] [warn] mod_wsgi: Runtime using Python/2.7.3.
[Sun Aug 18 19:46:05 2013] [notice] Apache/2.2.22 (Ubuntu) mod_wsgi/3.3 Python/2.7.3 configured -- resuming normal operations
sh: line 0: source: sourceme: file not found

我不明白这是怎么回事。我在/usr/locals/src/文件夹中清楚的有sourceme文件,我用ls验证了很多次。我不知道发生了什么。我使用 os.getcwd() 检查目录是否被正确更改,但仍然找不到文件。如果我遗漏了什么,请告诉我,因为我对这个我无法意识到的问题感到非常沮丧。 谢谢

最佳答案

您的方法目前没有意义: os.system 创建 WSGI 进程的子进程,同时将父进程的环境传递给子进程,子进程的环境不会传回/与父进程共享。

此外,source 不是二进制文件,而是 shell 本身的内置函数。 如果你想设置环境变量,我们可以使用 os.environ,这是一个变量字典。

由于您使用的是 python,因此使用 python 程序而不是某些 os.system 调用会更明智。也就是说,使用环境变量创建 python 程序,将其添加到您的 PYTHONPATH 并将其导入到您的 django 代码中。

关于android - Python 系统调用找不到文件,在 Apache 服务器上运行出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18303093/

相关文章:

python - 扩展 python 列表以在追加之前检查输入

Django:x 天后发送电子邮件

python - Django、uwsgi、nginx、virtualenv、ImportError : No module named site

python - 得到一个 'AttributeError at/login/authenticated' , 'User' 对象没有属性 'backend'

android - 执行 Android 项目时出错 "Activity class does not exist."

android - 如何在 Android 中实现交错行布局?

android - 尝试使用异步任务运行服务时 ProgressDialog 卡住

android - 持久服务

python - 如何使用 OpenCV 在一张 RGB 图像中编码灰度、SobelX 和 SobelY?

EC2 上的 Python 多线程?