从html执行python脚本
浏览器显示代码,而不是运行python脚本 HTML代码如下所示:从html执行python脚本,python,html,cgi,Python,Html,Cgi,浏览器显示代码,而不是运行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="U
<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>
#!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代码如下所示:
<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>
#!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,)
#!c:\Python27\python.exe
#!/usr/bin/python
从google.appengine.ext导入webapp
从google.appengine.ext.webapp.util导入运行\u wsgi\u应用程序
导入cgi,操作系统
进口cgib;cgib.enable()
try:#Windows需要为二进制模式设置stdio。
导入msvcrt
msvcrt.setmode(0,os.O_二进制)#stdin=0
msvcrt.setmode(1,os.O_二进制)#stdout=1
除恐怖外:
通过
form=cgi.FieldStorage()
#嵌套的FieldStorage实例保存该文件
fileitem=form['file']
#测试文件是否已上载
如果为fileitem.filename:
#从文件名中剥离前导路径以避免目录遍历攻击
fn=os.path.basename(fileitem.filename)
打开(os.path.join('static/files/'+fn,'wb')).write(fileitem.file.read())
消息='文件'+fn+'已成功上载'
其他:
消息='未上载任何文件'
打印“”\
内容类型:text/html\n
%
“%”(消息,)
我尝试过论坛上发布的各种解决方案,但没有一个对我有效。请指导我哪些地方需要纠正。谢谢大家的快速帮助。我刚刚遇到了这个问题,我也遇到了同样的问题。你现在有答案了吗
至于代码不起作用的原因,是因为HTML没有将其解释为python文件。这是我迄今为止所理解的,因为我还没有解决我的问题。您需要某种html解释器来理解python文件。您使用的是哪种web服务器?我认为您必须将其配置为使用Python脚本将文件夹标记为cgi binI。我正在google app engine上部署此代码。我对Python非常陌生,我帮助一位正在紧急休假的同事。你能解释一下我该怎么做吗。