不能跑瓶装水

2024-05-13 07:31:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python和BottlePy是个新手,所以这个问题可能看起来很愚蠢。我无法运行我的BottlePy文件,因为Python窗口在应该运行BottlePy时会消失。 有人知道为什么Python不运行我的代码吗?你知道吗

这是我的blottlepy代码(我使用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)

Tags: oftheto代码infromimportjson