在apache网络服务器中执行python cgi脚本时出现问题

utugiqy6  于 2022-12-28  发布在  Python
关注(0)|答案(2)|浏览(163)

我已经搜索和阅读了其他有类似问题的帖子。我修复了.py文件中的shebang行,并确保httpd.conf有正确的配置。不幸的是,没有任何东西解决我的问题,我仍然得到可怕的错误-

[Mon Jun 01 10:37:02.994516 2020] [cgi:error] [pid 19596:tid 1196] (OS 1920)The file cannot be accessed by the system.  : [client ::1:50159] couldn't create child process: 721920: upload_file.py
[Mon Jun 01 10:37:02.994516 2020] [cgi:error] [pid 19596:tid 1196] (OS 1920)The file cannot be accessed by the system.  : [client ::1:50159] AH01223: couldn't spawn child process: C:/Users/raj_d/webroot/tsp_quick/cgi-bin/upload_file.py

Python脚本-

#!"C:\Users\raj_d\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\python.exe"
import cgi, os
import cgitb

cgitb.enable()
form = cgi.FieldStorage()
# Get filename here.
fileitem = form['filename']
# Test if the file was uploaded
if fileitem.filename:
   # strip leading path from file name to avoid
   # directory traversal attacks
   fn = os.path.basename(fileitem.filename)
   open('/tmp/' + fn, 'wb').write(fileitem.file.read())
   message = 'The file "' + fn + '" was uploaded successfully'
else:
   message = 'No file was uploaded'
print """\
Content-Type: text/html\n
<html>
<body>
   <p>%s</p>
</body>
</html>
""" % (message,)

我玩过在shebang的完整路径上添加和删除引号的游戏,但这并没有帮助。
我在httpd.conf里有这些-

LoadModule cgi_module modules/mod_cgi.so

<Directory "C:\Users\raj_d\webroot\tsp_quick\cgi-bin">
    AllowOverride None
    # Options None
    Options +ExecCGI
    AddHandler cgi-script .py
    Require all granted
</Directory>

我确定巨蟒是从蛇帮路上被驱逐出去的-

C:\>C:\Users\raj_d\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\python.exe
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

任何帮助都将不胜感激。
先谢了。
研发部

dauxcl2d

dauxcl2d1#

好吧,我想通了。
首先,也是最重要的,一个人应该始终遵循指导方针:)
在我的例子中,我太快放弃了从命令行运行脚本。我从Microsoft商店安装了Python 3.8.3,出于某种原因,每次我从命令行运行脚本时,都会弹出一个新的cmd窗口并很快关闭。我无法改变这种行为,也看不到哪里出了问题。
所以我卸载了python,从python的网站下载了新的安装程序,安装在一个安装我的开发工具的公共位置,现在,我可以从命令行运行我的脚本,并能够看到实际的问题是什么。
原来是print!下面是修改后的代码,它工作得很好,而且shebang也变得更简单了。

#!C:\tools\python\\python.exe
import cgi, os
import cgitb
import time

cgitb.enable()
form = cgi.FieldStorage()
# Get filename here.
fileitem = form['filename']
# Test if the file was uploaded
if fileitem.filename:
   # strip leading path from file name to avoid
   # directory traversal attacks
   fn = os.path.basename(fileitem.filename)
   # open('/tmp/' + fn, 'wb').write(fileitem.file.read())
   message = 'The file "' + fn + '" was uploaded successfully'
else:
   message = 'No file was uploaded'
a = """Content-Type: text/html\n
<html>
<body>
<p>{}</p>
</body>
</html>
"""
print (a.format(message))
jtoj6r0c

jtoj6r0c2#

我在Windows上尝试在Apache中执行 *.py文件时遇到了类似的问题。我遵循了Apache CGI安装指南,但问题(正如您提到的)是Python可执行文件

C:/Users/../AppData/Local/Microsoft/WindowsApps/Python/

由于某些原因,Apache不允许您在这个位置使用Python解释器,我按照您的建议卸载了Python 3.10.x并重新安装在我的D:驱动器和嘿presto,它刚刚工作。我怀疑这是一个windows唯一的怪癖,可能是一个安全问题。

相关问题