【问题标题】:execute python scripts from html从 html 执行 python 脚本
【发布时间】:2014-04-21 13:04:05
【问题描述】:

浏览器显示代码而不是运行 python 脚本。

HTML代码如下:

<html><body>
<form enctype="multipart/form-data" action="/static/savefiles.py" method="post">
<p>File: <input type="file" name="file"></p>
<p><input type="submit" value="Upload"></p>
</form>
</body></html>

Python代码如下:

#!c:\Python27\python.exe
#!/usr/bin/python


from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import cgi, os
import cgitb; cgitb.enable()



        try: # Windows needs stdio set for binary mode.
            import msvcrt
            msvcrt.setmode (0, os.O_BINARY) # stdin  = 0
            msvcrt.setmode (1, os.O_BINARY) # stdout = 1
        except ImportError:
            pass

        form = cgi.FieldStorage()

        # A nested FieldStorage instance holds the file
        fileitem = form['file']

        # 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(os.path.join('static/files/' + 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,)

我尝试了论坛上发布的各种解决方案,但没有一个对我有用。请指导我需要更正的内容。感谢大家的快速帮助。

【问题讨论】:

  • 您使用的是哪种网络服务器?我认为您必须对其进行配置以将带有 Python 脚本的文件夹标记为 cgi-bin
  • 我正在谷歌应用引擎上部署这段代码。我对 Python 非常陌生,并且正在帮助一位正在紧急休假的同事。你能解释一下我到底该怎么做。

标签: python html cgi


【解决方案1】:

我只是偶然发现了这个问题,我也遇到了同样的问题。你现在有答案了吗?

至于为什么您的代码不起作用是因为 HTML 没有将其解释为 python 文件。到目前为止,这是我所了解的,因为我还没有解决我的问题。你需要某种 html 解释器来理解 python 文件。

【讨论】:

    猜你喜欢
    • 2018-07-11
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 2023-03-15
    • 2012-11-28
    • 1970-01-01
    相关资源
    最近更新 更多