我是Python和BottlePy的新手,所以这个问题可能看起来很愚蠢。我无法运行我的BottlePy文件,因为Python-window在运行BottlePy时会消失。 有谁知道Python为什么不运行我的代码?
这是我的BottlePy代码(我使用Notepad ++编辑代码)
from bottle import response, error, get
import json
@get('/hello')
def hello_world():
'''Responds to http://localhost:8080/hello with an example JSON object
'''
response_body = {'Hello': 'World'}
# This returns valid JSON in the response, but does not yet set the
# associated HTTP response header. This you should do yourself in your
# own routes!
return json.dumps(response_body)
@get('/db-example')
def db_example(db):
'''Responds with names of all products in Amsterdam
Add a parameter 'db' to your function to get a database cursor from
WtPlugin. The parameter db is of type sqlite3.Cursor. Documentation is
at https://docs.python.org/2/library/sqlite3.html#sqlite3.Cursor
If you want to start with a clean sheet, delete the file 'inventory.db'.
It will be automatically re-created and filled with one example item.
Access this route at http://localhost:8080/db-example
'''
# Execute SQL statement to select the name of all items located in A'dam
db.execute("SELECT name FROM inventory WHERE location=?", ('Amsterdam',))
# Get all results in a list of dictionaries
names = db.fetchall() # Use db.fetchone() to get results one by one
# TODO: set the appropriate HTTP headers and HTTP response codes.
# Return results as JSON
return json.dumps(names)
@error(404)
def error_404_handler(e):
# Content type must be set manually in error handlers
response.content_type = 'application/json'
return json.dumps({'Error': {'Message': e.status_line, 'Status': e.status_code}})
if __name__ == "__main__":
from bottle import install, run
from wtplugin import WtDbPlugin, WtCorsPlugin
install(WtDbPlugin())
install(WtCorsPlugin())
run(host='localhost', port=8080, reloader=True, debug=True, autojson=False)