如何在网页中运行python脚本

Har*_*rma 51 python

我是python的新手.只知道什么是python.我创建了以下代码(在python IDLE中):

print "Hi Welcome to Python test page\n";
print "Now it will show a calculation";
print "30+2="; print 30+2;
Run Code Online (Sandbox Code Playgroud)

然后我将此页面保存在我的localhost中作为index.py

我使用http://localhost/index.py运行脚本

但它没有显示执行的python脚本.相反,它将上述代码显示为HTML.问题出在哪儿?请有人告诉我如何在网页上运行python?

Uku*_*kit 42

为了显示您的代码,您需要以下几点:

首先,需要有一个处理HTTP请求的服务器.目前,您只是在本地硬盘上使用Firefox打开文件.需要像Apache或类似的服务器.

其次,假设您现在有一个服务器来提供文件,您还需要一些能够将代码解释为服务器的Python代码的东西.对于Python用户来说,解决方案现在是mod_wsgi.但是对于更简单的情况,你可以坚持使用CGI(这里有更多信息),但如果你想轻松制作网页,你应该使用像Django这样的现有Python网页框架.

设置这个可能非常麻烦,所以要做好准备.

  • 在当今时代,CGI 正变得越来越像恐龙。如果您熟悉 CGI,请务必使用它;但如果您才刚刚开始,找到一个简单的框架可能是获得其他人和 Google 帮助的阻力最小的途径。 (2认同)

jpa*_*jpa 21

正如其他人所指出的,Python有许多Web框架.

但是,看到你刚刚开始使用Python,一个简单的CGI脚本可能更合适:

  1. 将脚本重命名为index.cgi.您还需要执行chmod +x index.cgi以赋予其执行权限.

  2. 在文件的开头添加以下两行:

#!/usr/bin/python   
print('Content-type: text/html\r\n\r')

在此之后,Python代码应该像终端一样运行,除了输出到浏览器.当你开始工作时,你可以使用cgi模块从浏览器中获取数据.

注意:这假设您的Web服务器正在运行Linux.对于Windows,#!/Python26/python可能会工作.

  • 对于我正在使用的网络托管,我不得不添加`#!/ usr/bin/env python# - * - 编码:UTF-8 - * - `. (3认同)
  • 调用文件 `index.cgi` 而不是 `index.py` 可能是错误的。随意称呼它——重要的是配置 Web 服务器以了解它是 CGI 脚本而不是静态内容。 (2认同)
  • 对实际问题的一个比已接受的答案更好的答案 (2认同)

Ash*_*yay 11

在Python中使用flask库可以实现这一点.记得将HTML页面存储到运行python脚本的名为"templates"的文件夹中.

所以你的文件夹看起来像

  1. templates(包含HTML文件的文件夹)
  2. 你的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()
Run Code Online (Sandbox Code Playgroud)

这是一个使用的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>
Run Code Online (Sandbox Code Playgroud)

这是一个小小的方法,通过它您可以完成比较两个字符串的简单任务,并且可以轻松更改以满足您的要求

  • 我们应该将Python和HTML文件命名为什么? (3认同)

ytp*_*lai 9

如果您使用自己的计算机,请安装名为XAMPP的软件(或WAMPP可以使用).这基本上是一个只能在您的计算机上运行的网站服务器.然后,安装完成后,转到xampp文件夹并双击htdocs文件夹.现在您需要做的是创建一个html文件(我将其称为runpython.html).(记得将python文件移动到htdocs)

将此添加到您的html正文(并根据需要输入)

<form action = "file_name.py" method = "POST">
   <input type = "submit" value = "Run the Program!!!">
</form>
Run Code Online (Sandbox Code Playgroud)

现在,在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"
Run Code Online (Sandbox Code Playgroud)