【问题标题】:Keep getting 404 errors in Bottle在 Bottle 中不断收到 404 错误
【发布时间】:2023-03-11 12:17:01
【问题描述】:

我正在尝试运行此 website 上提供的示例

我的代码是这样的

#!/usr/bin/env python3

from bottle import route, run

@route('/cars')
def getcars():

    cars = [ {'name': 'Audi', 'price': 52642},
        {'name': 'Mercedes', 'price': 57127},
        {'name': 'Skoda', 'price': 9000},
        {'name': 'Volvo', 'price': 29000},
        {'name': 'Bentley', 'price': 350000},
        {'name': 'Citroen', 'price': 21000},
        {'name': 'Hummer', 'price': 41400},
        {'name': 'Volkswagen', 'price': 21600} ]

    return dict(data=cars)


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

当我运行 python run.py 并登录到http://localhost:8080/cars。我收到 404 错误。当我运行以下示例时,我得到相同的 404 错误

#!/usr/bin/env python3

from bottle import route, run

@route('/message')
def hello():
    return "Today is a beautiful day"  

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

我错过了什么?

【问题讨论】:

  • 这适用于 python3.6.2
  • 我实际使用的是 3.7
  • 它也适用于 3.7。您确定(抱歉)在 url 末尾写 /cars 吗? (也许您单击控制台中没有它的链接)。控制台打印什么?
  • 我在vs code中使用了集成终端。那没起效。当我在 Win 10 中使用普通终端窗口时它可以工作
  • 也许它正在调用一个没有安装瓶子的不同 python 版本......

标签: python python-3.6 bottle


【解决方案1】:

我有python2.7。我也试过了,我得到了正确的输出

{"data": [{"price": 52642, "name": "Audi"}, {"price": 57127, "name": "Mercedes"}, {"price": 9000, "name": "Skoda"}, {"price": 29000, "name": "Volvo"}, {"price": 350000, "name": "Bentley"}, {"price": 21000, "name": "Citroen"}, {"price": 41400, "name": "Hummer"}, {"price": 21600, "name": "Volkswagen"}]}

【讨论】:

    【解决方案2】:

    我会使用@get 和@post 而不仅仅是@route。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      相关资源
      最近更新 更多