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

2024-06-24 13:23:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我搜索和阅读过其他类似问题的帖子。我修复了.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>

我确保python是可以从shebang路径中执行的-

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.
>>>

任何帮助都将不胜感激

提前谢谢

研发部


Tags: thepymessagehtmlfilenameusersfilefn
1条回答
网友
1楼 · 发布于 2024-06-24 13:23:28

嗯,我知道了

首先也是最重要的是,我们应该始终遵循以下准则:)

在我的例子中,我太快放弃了从命令行运行脚本。我从Microsoft应用商店安装了Python 3.8.3,出于某种原因,每次我从命令行运行脚本时,都会弹出一个新的cmd窗口并很快关闭。我无法改变这种行为,也看不出哪里出了问题

因此,我卸载了python,从python的站点下载了新的安装程序,并安装在安装我的开发工具的公共位置。现在,我可以从命令行运行我的脚本,并且能够看到实际的问题是什么

原来是打印的!下面是修改后的代码,它工作得很好,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))

相关问题 更多 >