首页 > 解决方案 > 单击 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>

标签: pythonbottle

解决方案


不应将cgiandcgitb与 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 风格相同,但更受欢迎且更受维护。


推荐阅读