我对使用HTML的python脚本有疑问。
我是新手。
我的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()
我将它作为localhost运行。 单击SEND时的输出(在我的html中,它等于我的语言中的ODOSLAT): 找不到
在服务器上找不到请求的URL。如果您输入了URL 请手动检查您的拼写,然后重试。
浏览器搜索中的文本字段现在如下所示:
http://127.0.0.1:5000/app/get.cgi?name=asdf&email=julo.marko%40gmail.com&message=asfasfasf
答案 0 :(得分:1)
如果您有Flask Web应用程序,则不需要任何.cgi脚本。只需将您的表格指向烧瓶中的适当路线即可。使用你的html中的url:
@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)