不断在Bottle中收到404错误

时间:2018-09-28 16:11:50

标签: python python-3.6 bottle

我正在尝试运行此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)

我想念什么?

2 个答案:

答案 0 :(得分:0)

我有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"}]}

答案 1 :(得分:0)

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