【问题标题】:How to write python script in flask如何在烧瓶中编写python脚本
【发布时间】:2016-04-05 16:28:10
【问题描述】:

我的 Python 脚本与 HTML 有关。

我是新手。

我的 python 脚本如下所示:

#!/usr/bin/python3.4

# Import modules for CGI handling 
import cgi, cgitb 

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
first_name = form.getvalue('first_name')
last_name  = form.getvalue('last_name')

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>Hello %s %s</h2>" % (first_name, last_name))
print("</body>")
print("</html>")

还有我的 HTML 文件:

<form action="hello_get.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>

我运行 HTML 并写入输入名字和姓氏,但我的脚本没有启动。如果我想打开脚本或保存它,它只是显示窗口。

编辑:

我的 HTML:

<div class="container">
    <div class="row">
        <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
            <p>Want to get in touch with me? Fill out the form below to send me a message and I will try to get back to you within 24 hours!</p>
            <!-- Contact Form - Enter your email address on line 19 of the mail/contact_me.php file to make this form work. -->
            <!-- WARNING: Some web hosts do not allow emails to be sent through forms to common mail hosts like Gmail or Yahoo. It's recommended that you use a private domain email address! -->
            <!-- NOTE: To use the contact form, your site must be on a live web host with PHP! The form will not work locally! -->

            <form action="/app/get.cgi" method="get">
                <div class="row control-group">
                    <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Name</label>
                            <input type="text" name="name" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
                            <p class="help-block text-danger"></p>
                    </div>
                </div>
                <div class="row control-group">
                    <div class="form-group col-xs-12 floating-label-form-group controls">
                        <label>Email Address</label>
                        <input type="email" name="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
                        <p class="help-block text-danger"></p>
                    </div>
                </div>
                <div class="row control-group">
                    <div class="form-group col-xs-12 floating-label-form-group controls">
                        <label>Message</label>
                        <textarea rows="5" name="message" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
                        <p class="help-block text-danger"></p>
                    </div>
                </div>
                <br>
                <div id="success"></div>
                <div class="row">
                    <div class="form-group col-xs-12">
                        <button type="submit" class="btn btn-default">Odoslať</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

(我的python脚本)get.cgi:

#!/usr/bin/python3.4

# Import modules for CGI handling
import cgi, cgitb
file = open('file.txt', mode='w')
form = cgi.FieldStorage()

name = form.getvalue('name')
email = form.getvalue('email')
message = form.getvalue('message')

file.write(name) 
file.write(email)
file.write(message)

file.close()

我的 app.py 是主要的烧瓶模块

from flask import Flask, render_template, request

app = Flask(__name__)
app.secret_key = 'mypa$$w0rd'

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

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


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


@app.route('/post.html')
def sample_post():
    return render_template('post.html')

if __name__ == '__main__':
    app.run()

我将它作为本地主机运行。 当我点击发送时输出(在我的 html 中它等于我的语言中的 ODOSLAT): 未找到

在服务器上找不到请求的 URL。如果您输入了 URL 请手动检查您的拼写,然后重试。

浏览器搜索中的文本字段现在如下所示:

http://127.0.0.1:5000/app/get.cgi?name=asdf&email=julo.marko%40gmail.com&message=asfasfasf

【问题讨论】:

  • 您是否设置了 Web 服务器来运行 python 脚本?您的脚本是否标记为可执行文件?脚本通常以.cgi 扩展名命名。你试过了吗?
  • @JohnGordon 请看我编辑的帖子。
  • @Reti43 请看我编辑的帖子。
  • 您的 get.cgi 脚本似乎是完全独立的,与 Flask 没有连接。如果您的网络服务器期望 Flask 处理所有 URL,那么这将解释 Not Found 错误。
  • 另外,您的 HTML 有 &lt;form action="/app/get.py"。该文件的实际名称是什么? .py 还是 .cgi?它位于哪里?是在$WEBROOT/app/吗?

标签: python html input flask cgi


【解决方案1】:

如果您有 Flask Web 应用程序,则不需要任何 .cgi 脚本。只需将您的表格指向烧瓶中的适当路线。使用您的 html 中的网址:

@app.route('/api/get.cgi')
def api_get():
    first_name = request.args.get('first_name')
    last_name = request.args.get('last_name')
    return """<html><body><h2>Hello %s %s</h2></body></html>""" % (first_name, last_name)

【讨论】:

  • 可能是题外话,但它在此代码中并且不起作用,为什么它不将其写入我的 get.cgi 脚本中定义的 file.txt?
  • /api/get.cgi 只是一个名称,与任何名为get.cgi 的文件无关。如果您需要其他操作,则必须将其写入烧瓶应用程序的函数中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多