【问题标题】:problem executing a python cgi script in apache webserver在 apache 网络服务器中执行 python cgi 脚本的问题
【发布时间】:2020-06-01 15:02:09
【问题描述】:

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

任何帮助将不胜感激。

提前致谢。

研发

【问题讨论】:

    标签: python apache scripting cgi


    【解决方案1】:

    好吧,我想通了。

    首先,一个人应该始终遵循准则:)

    就我而言,我放弃了从命令行运行脚本的速度太快了。我从 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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-11
      • 1970-01-01
      • 2012-07-27
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      相关资源
      最近更新 更多