【发布时间】: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 有
<form action="/app/get.py"。该文件的实际名称是什么?.py还是.cgi?它位于哪里?是在$WEBROOT/app/吗?
标签: python html input flask cgi