Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将输入从html传递到python并返回_Python_Html_Python 3.x_Flask_Cgi - Fatal编程技术网

将输入从html传递到python并返回

将输入从html传递到python并返回,python,html,python-3.x,flask,cgi,Python,Html,Python 3.x,Flask,Cgi,我需要为作业制作一个网页,它不需要上传到网上,我只是使用一个本地的.html文件。 我进行了一些阅读,得出了以下html和python: <!DOCTYPE html> <html> <head> <title> CV - Rogier </title> </head <body> <h3>

我需要为作业制作一个网页,它不需要上传到网上,我只是使用一个本地的.html文件。 我进行了一些阅读,得出了以下html和python:

<!DOCTYPE html>
<html>
    <head>
        <title>
            CV - Rogier
        </title>
    </head
    <body>
        <h3>
            Study
        </h3>
        <p>
            At my study we learn Python.<br>
            This is a sall example:<br>
            <form action="/cgi-bin/cvpython.py" method="get">
                First Name: <input type="text" name="first_name">  <br />
                Last Name: <input type="text" name="last_name" />
                <input type="submit" value="Submit" />
            </form>
        </p>
    </body>
</html>

罗吉尔

你有像apache这样的web服务器吗? 如果您不这样做,我不确定这是否可行,因此您可能需要查看一下,以允许它执行python脚本,您还需要编辑
httpd.conf
文件

发件人:

更新:

如果没有本地web服务器设置,另一种选择是用作web服务器。 您需要按照以下方式组织项目:

    /yourapp  
    basic_example.py  
    /static/  
        /test.css
    /templates/  
        /test.html  
Python:

import cgi
import cgitb #found this but isn't used?

form = cgi.FieldStorage()

first_name = form.getvalue('first_name').capitalize()
last_name  = form.getvalue('last_name').capitalize()

print ("Content-type:text/html\r\n\r\n")
print ("<html>")
print ("<head>")
print ("<title>Hello - Second CGI Program</title>")
print ("</head>")
print ("<body>")
print ("<h2>Your name is {}. {} {}</h2>".format(last_name, first_name, last_name))
print ("</body>")
print ("</html>")
__author__ = 'kai'

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('test.html')

@app.route('/hello', methods=['POST'])
def hello():
    first_name = request.form['first_name']
    last_name = request.form['last_name']
    return 'Hello %s %s have fun learning python <br/> <a href="/">Back Home</a>' % (first_name, last_name)

if __name__ == '__main__':
    app.run(host = '0.0.0.0', port = 3000)
根据你的问题做了一个基本的例子
希望这能帮助您走上正轨,祝您好运。

在apache/conf/httpd.conf文件中更改以下内容:

<Directory "C:/xampp/cgi-bin"> 
    AllowOverride All
    Options None
    Require all granted
</Directory>

允许超越所有
选项无
要求所有授权
致:


选项执行CGI
AddHandler cgi script.cgi.py
命令允许,拒绝
通融
否则,也可以使用python脚本运行html表单:(无需更改上面的http.conf文件)

#!C:\Users\DELL\AppData\Local\Programs\Python\Python36\Python.exe
打印(“内容类型:text/html\r\n\r\n”)
打印(“”)
打印(“”)
打印(“”)
打印(“名字:
”) 打印('姓氏:') 打印(“”) 打印(“”) 打印(“”) 打印(“”)`
我对HTML和Python都是新手,但据我所知,HTML不必缩进。这意味着您可以在一次python打印中传递大量HTML代码。 实际上,您可以在一行中打印整个HTML代码

我正在使用for循环将列表打印到html表中。 HTML代码不是很好,但它工作正常。 我想你只需要它们是暂时的,就像我一样


干杯

首先检查浏览器url,如下所示 file:///C:/xampp/htdocs/cgi_python/form.html

这意味着您的代码是正确的,没有错误 但输出显示为python脚本代码 因为“代码在控制台中工作,而不是在浏览器中”

i、 e是指 您可以直接在htdocs文件夹中打开一个文件 然后单击filename.html并执行,然后以文本格式输出 用于打开浏览器

解决办法是

  • 在url中打开浏览器类型
  • 本地主机/文件路径 如。 然后得到答案

  • 在python中,换行符只是
    \n
    ,而不是
    \r\n
    。您使用什么浏览器?
    内容类型:text/html
    啊,是的,我复制了那一行,但将尝试使用
    /n
    。我用chrome@Peter木头能解决问题吗?(因为它没有)我不知道我想要什么,我只是尝试使用这个网站使它工作。我如何从我的主html文件调用/打开新的html文件?你有像apache这样的web服务器上的python脚本设置吗?目的是什么?你只是想让python创建一个网页吗?不,我需要创建一个html页面,不需要服务器。我希望能够在html页面上输入一个名称(或任何内容),并将其发送到python脚本,该脚本对其进行处理,然后将其转发回浏览器(无论是相同的选项卡还是新的选项卡)(但如果我愿意,我可以使用web服务器,我将尝试使用您的答案)您是对的,缩进在html中并不重要。你可以在一行中完成所有的事情。但是html的缩进或非缩进不是问题。这是webserver设置(请参阅接受的答案)。对不起,我知道,我只是想添加它,因为有人看到他一行一行地写,这很痛苦。但我还不能评论。你说得对,发表评论会更好。当然,没有足够的观点发表评论是一个非常合理的理由:-)无论如何欢迎来到stackoverflow!过一会儿,你就会得到你需要的分数。
    from yattag import Doc
    
    doc, tag, text = Doc().tagtext()
    
    with tag('h1'):
        text('Hello world!')
    
    print(doc.getvalue())
    
        /yourapp  
        basic_example.py  
        /static/  
            /test.css
        /templates/  
            /test.html  
    
    __author__ = 'kai'
    
    from flask import Flask, render_template, request
    
    app = Flask(__name__)
    
    @app.route('/')
    def index():
        return render_template('test.html')
    
    @app.route('/hello', methods=['POST'])
    def hello():
        first_name = request.form['first_name']
        last_name = request.form['last_name']
        return 'Hello %s %s have fun learning python <br/> <a href="/">Back Home</a>' % (first_name, last_name)
    
    if __name__ == '__main__':
        app.run(host = '0.0.0.0', port = 3000)
    
    <!DOCTYPE html>
    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="static/test.css">
            <title>
                CV - Rogier
            </title>
        </head>
        <body>
            <h3>
                Study
            </h3>
            <p>
                At my study we learn Python.<br>
                This is a sall example:<br>
                <form action="/hello" method="post">
                    First Name: <input type="text" name="first_name">  <br />
                    Last Name: <input type="text" name="last_name" />
                    <input type="submit" name= "form" value="Submit" />
                </form>
            </p>
        </body>
    </html>
    
    p {
        font-family: verdana;
        font-size: 20px;
    }
    h2 {
        color: navy;
        margin-left: 20px;
        text-align: center;
    }
    
    <Directory "C:/xampp/cgi-bin"> 
        AllowOverride All
        Options None
        Require all granted
    </Directory>
    
      <Directory "C:/xampp/cgi-bin">
      Options ExecCGI
      AddHandler cgi-script .cgi .py
      Order allow,deny
      Allow from all
      </Directory>
    
    #!C:\Users\DELL\AppData\Local\Programs\Python\Python36\Python.exe
    
    print("Content-type:text/html \r\n\r\n")
    print('<html>')
    print('<body>')
    print('<form action="/cgi-bin/hello_get.py">')
    print('First Name:<input type = "text" name = "first_name"><br/>')
    print('Last Name:<input type = "text" name = "last_name" />')
    print('<input type = "submit" value = "submit">')
    print('</form>')
    print('</body>')
    print('</html>')`