python - 单击 HTML 按钮时如何运行脚本(Python、Bottle)
问题描述
我想在按下带有瓶子的按钮时运行一个脚本。但我每次都会收到 404 错误。它在地址栏中显示 localhost: //File.py,但我不知道如何路由它。
应用程序.py
from bottle import *
@route('/')
def home():
return template('deneme.html')
run(host='localhost',port=8080)
文件.py
#!/usr/bin/python
import cgi, cgitb
form = cgi.FieldStorage
username = form["username"].value
emailaddress = form["emailaddress"].value
print("Content-type: text/html\r\n\r\n")
print( "<html>")
print("<head>")
print("<title>First Script</tittle>")
print("</head")
print("<body>")
print("<h3>This is HTML's Body Section</h3>")
print(username)
print(emailaddress)
print("</body>")
print("</html>")
deneme.html
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="File.py" method="post">
username: <input type="text" name="username"/>
<br />
Email Adress: <input type="email" name="emailaddress"/>
<input type="submit" name="Submit">
</form>
</body>
</html>
解决方案
不应将cgi
andcgitb
与 Bottle、Flask 或任何其他 Python Web 框架一起使用。
尝试类似的东西
from bottle import run, route, request
@route('/')
def home():
return template('deneme.html')
@route('/foo')
def foo():
return '%s %s' % (request.forms.username, request.forms.email)
run(host='localhost',port=8080)
(并将表单的操作更改为action="/foo"
)。
另外,考虑使用 Flask;它与 Bottle 风格相同,但更受欢迎且更受维护。
推荐阅读
- r - ggplot2 annotation_ticks 在绘图区域的外部
- android - Android:仅从左侧模糊图像
- python - python中导入目录文件的机制
- android - 从 SQLite 迁移到 Room:迁移没有正确处理表
- raspberry-pi3 - 树莓派 pi3 无法在主 HDMI 显示器上启动应用程序,可以使用 ssh -x 在远程启动
- msbuild - 无法通过构建定义检测错误
- python - Matplotlib:空白图和窗口不会关闭
- orm - 阿多尼斯`belongsToMany`关系不起作用
- snakemake - 在继续执行下一个规则之前,如何让 Snakemake 将所有样本应用于单个规则?
- android - 带有 buildSrc 编译错误的 Kotlin Gradle DSL