python:无法从webbrowser调用文件中的函数
我想在python中使用瓶子框架创建一个RESTful web服务。其中我有一个CSV文件,其中包含lat和lan的数据。每次有人从文件中搜索特定的邮政编码时,我都必须获取这些数据并将其显示在浏览器上。 到目前为止,我已经做到了python:无法从webbrowser调用文件中的函数,python,csv,bottle,Python,Csv,Bottle,我想在python中使用瓶子框架创建一个RESTful web服务。其中我有一个CSV文件,其中包含lat和lan的数据。每次有人从文件中搜索特定的邮政编码时,我都必须获取这些数据并将其显示在浏览器上。 到目前为止,我已经做到了 from bottle import route, run, request @route('/') def index(): """ Display welcome & instruction messages """ return "&
from bottle import route, run, request
@route('/')
def index():
""" Display welcome & instruction messages """
return "<p>Welcome to my extra simple bottle.py powered server!</p> \
<p>The Web service can find a location from csv file \
The way to invoke is :\
<ul> \
<li>http://localhost:8080/getlocation?postcode=xxxx</li>\
</ul> \
xxxx are the postcode you want to search."
@route('/getlocation/<postcode>')
def getlocation(postcode):
csv_file = csv.reader(open('clinic_locations.csv', "r"), delimiter=",")
#return{clinicname, latitude, longitude, email, state}
for row in csv_file:
if postcode == row[6]:
return{row[3], row[8], row[9], row[7], row[5]}
run(host='localhost', port=8080, debug=True)
我不知道我错在哪里。
谁能帮帮我 您的路线是/getlocation/,而不是/getlocation?邮政编码=。您似乎在浏览器中执行此操作:
http://localhost:8080/getlocation?postcode=4000
您是否尝试过:
http://localhost:8080/getlocation/4000
下面有两个示例,路由[1]中的动态参数和查询参数[2]: 使现代化 要测试上述代码: 除方法外: 第一种方法: 第二种方法:
这没有解决我的问题。很抱歉,请求的URL导致错误:找不到:“/getlocation”更新代码后是否重新启动服务?是的,我每次都这样做。再次出现相同的错误。另外,我需要的url是这种格式的getlocation?postcode=xxxxI重写我的代码和你的解决方案工作。但同样的,它不是我需要的格式。也许这会有帮助吗?:从这里看来,你只需要@route'/getlocation'谢谢。非常感谢。请查看更新。并让我知道我的回答是否有助于解决问题?这对我很有帮助。谢谢,先生。
http://localhost:8080/getlocation/4000
import json
from bottle import route, request, response, run, template, abort, error
@error(500)
@error(404)
@error(400)
def error_handler(error):
try:
error_details = {"error_code": error.status_code, "message": error.body}
except:
error_details = {"error_code": 500, "message": error.exception}
return json.dumps(error_details)
"""
Example of try catch with bottle abort to handle
custom error messages in the 'error_handler' above.
"""
@route("/")
def index():
try:
raise Exception('A generic exception')
except Exception as ex:
abort(500, ex.args[0])
@route("/greet/<name>")
def greet(name):
return template("Greetings <b>{{name}}</b>", name=name)
@route("/greet")
def greet():
if 'name' not in request.query:
abort(400, "'name' parameter is missing...")
return template("Greetings <b>{{name}}</b>", name=request.query['name'])
run(host='localhost', port=8080)