单击 HTML 按钮时如何 运行 编写脚本(Python,Bottle)

How to run script when HTML button is clicked (Python, Bottle)

我想 运行 按下带有瓶子的按钮时的脚本。但我每次都会收到 404 错误。它在地址栏中显示 localhost://File.py,但我不知道如何路由它。

app.py

from bottle import *

@route('/')
def home():
    return template('deneme.html')


run(host='localhost',port=8080)

File.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>

您不应将 cgicgitb 与 Bottle、Flask 或任何其他 Python 网络框架一起使用。

试试

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 一脉相承,但更受欢迎,也更易于维护。