如何在网页中运行 Python 脚本
- 2025-03-20 08:48:00
- admin 原创
- 38
问题描述:
我已经创建了以下代码(在 Python IDLE中):
print "Hi Welcome to Python test page
";
print "Now it will show a calculation";
print "30+2=";
print 30+2;
然后我将此页面作为index.py保存在我的本地主机中
我使用运行脚本
但它没有显示执行的 Python 脚本。相反,它将上述代码显示为 HTML。问题出在哪里?如何在网页中运行 Python 文件?
解决方案 1:
为了显示你的代码,你需要几样东西:
首先,需要有一个处理 HTTP 请求的服务器。目前,您只是使用 Firefox 在本地硬盘上打开文件。需要像 Apache 或类似服务器。
其次,假设您现在有一台服务器来提供文件,您还需要一些东西来将代码解释为服务器的 Python 代码。对于 Python 用户来说,现在的解决方案是 mod_wsgi。但对于更简单的情况,您可以坚持使用 CGI(更多信息请点击此处),但如果您想轻松制作网页,您应该使用现有的 Python Web 框架,如 Django。
设置这个可能会相当麻烦,所以要做好准备。
解决方案 2:
正如其他人指出的那样,Python 有很多 Web 框架。
但是,鉴于您刚刚开始使用 Python,一个简单的 CGI 脚本可能更合适:
将脚本重命名为
index.cgi
。您还需要执行chmod +x index.cgi
以赋予其执行权限。在文件开头添加以下两行:
/usr/bin/python 的 #!
打印('内容类型:text/html
')
此后,Python 代码应该像在终端中一样运行,只是输出会发送到浏览器。当您完成此操作后,您可以使用cgi 模块从浏览器获取数据。
注意:这假设您的网络服务器运行的是 Linux。对于 Windows,#!/Python26/python
可能会有效。
解决方案 3:
使用Python 中的Flask库,您可以实现这一点。请记住将您的 HTML 页面存储到运行 Python 脚本的名为“templates”的文件夹中。
所以你的文件夹看起来像
模板(包含 HTML 文件的文件夹)
你的 Python 脚本
这是您的 Python 脚本的一个小示例。它只是检查是否存在抄袭。
from flask import Flask
from flask import request
from flask import render_template
import stringComparison
app = Flask(__name__)
@app.route('/')
def my_form():
return render_template("my-form.html") # This should be the name of your HTML file
@app.route('/', methods=['POST'])
def my_form_post():
text1 = request.form['text1']
text2 = request.form['text2']
plagiarismPercent = stringComparison.extremelySimplePlagiarismChecker(text1,text2)
if plagiarismPercent > 50 :
return "<h1>Plagiarism Detected !</h1>"
else :
return "<h1>No Plagiarism Detected !</h1>"
if __name__ == '__main__':
app.run()
这是使用的 HTML 文件的小模板:
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Enter the texts to be compared</h1>
<form action="." method="POST">
<input type="text" name="text1">
<input type="text" name="text2">
<input type="submit" name="my-form" value="Check !">
</form>
</body>
</html>
这是一个小方法,通过它您可以完成比较两个字符串的简单任务,并且可以轻松更改以满足您的要求。
解决方案 4:
如果您使用的是自己的计算机,请安装一个名为 XAMPP(或WAMP都可以)的软件。这基本上是一个仅在您的计算机上运行的网站服务器。然后,安装完成后,转到xampp文件夹并双击htdocs文件夹。现在您需要创建一个 HTML 文件(我将它命名为runpython.html)。(记得将 Python 文件也移动到htdocs 。)
将其添加到您的 HTML 主体中(并根据需要添加输入)。
<form action = "file_name.py" method = "POST">
<input type = "submit" value = "Run the Program!!!">
</form>
现在,在 Python 文件中,我们基本上要打印出 HTML 代码。
# We will need a comment here depending on your server. It is basically telling the server where your python.exe is in order to interpret the language. The server is too lazy to do it itself.
import cgitb
import cgi
cgitb.enable() # This will show any errors on your webpage
inputs = cgi.FieldStorage() # REMEMBER: We do not have inputs, simply a button to run the program. In order to get inputs, give each one a name and call it by inputs['insert_name']
print "Content-type: text/html" # We are using HTML, so we need to tell the server
print # Just do it because it is in the tutorial :P
print "<title> MyPythonWebpage </title>"
print "Whatever you would like to print goes here, preferably in between tags to make it look nice"
解决方案 5:
如果有人正在寻找客户端:
Skulpt是 Python 在客户端运行的实现。非常有趣,不需要插件,只需简单的 JavaScript 代码。
解决方案 6:
根据您当前的需求,这将可行:
def start_html():
return '<html>'
def end_html():
return '</html>'
def print_html(text):
text = str(text)
text = text.replace('
', '<br>')
return '<p>' + str(text) + '</p>'
if __name__ == '__main__':
webpage_data = start_html()
webpage_data += print_html("Hi Welcome to Python test page
")
webpage_data += fd.write(print_html("Now it will show a calculation"))
webpage_data += print_html("30+2=")
webpage_data += print_html(30+2)
webpage_data += end_html()
with open('index.html', 'w') as fd: fd.write(webpage_data)
打开index.html文件,你就会看到你想要的内容。
扫码咨询,免费领取项目管理大礼包!