Python Flask JSON键错误
我正在尝试从另一个API服务获取一些JSON数据,并更新我的flask应用程序的数据库,同时用户可以下载一些PDF文件。该API有3个键。第一个是“状态”。当该“状态”键具有Python Flask JSON键错误,python,json,flask,keyerror,Python,Json,Flask,Keyerror,我正在尝试从另一个API服务获取一些JSON数据,并更新我的flask应用程序的数据库,同时用户可以下载一些PDF文件。该API有3个键。第一个是“状态”。当该“状态”键具有“success”值时,它还具有其他两个键和值。然后应用程序运行良好,没有错误。 但是,当“Status”具有'fail'值时,其他两个键和值将不存在。我写了一个异常,但它不起作用,结果出现了一个KeyError,KeyError:“country” 这是我的密码 @app.route("/pdf/dow
“success”
值时,它还具有其他两个键和值。然后应用程序运行良好,没有错误。
但是,当“Status”具有'fail'
值时,其他两个键和值将不存在。我写了一个异常,但它不起作用,结果出现了一个KeyError,KeyError:“country”
这是我的密码
@app.route("/pdf/download/<int:pdf_id>", methods=['GET', 'POST'])
def downloads(pdf_id):
current_ip = someIPaddress
req = requests.request("GET", 'http://anotherwebsite.com/json/someIPaddress?fields=169')
req_two = req.json()
status = req_two['status']
country = req_two['country']
city = req_two['city']
download_file = Pdf_info.query.get(pdf_id)
if Ipaddress.query.filter(Ipaddress.ip_address == current_ip, Ipaddress.pdfid == pdf_id).first():
try:
return send_from_directory("pdfs/pdf/", filename=download_file.file_location_name, as_attachment=True)
except FileNotFoundError:
abort(404)
else:
if status == "success":
ip_adding = Ipaddress(ip_address=current_ip, pdfid=pdf_id, downtime=datetime.utcnow(), country=country, location=city)
db.session.add(ip_adding)
db.session.commit()
try:
return send_from_directory("pdfs/pdf/", filename=download_file.file_location_name, as_attachment=True)
except FileNotFoundError:
abort(404)
else:
ip_adding = Ipaddress(ip_address=current_ip, pdfid=pdf_id, downtime=datetime.utcnow())
db.session.add(ip_adding)
db.session.commit()
try:
return send_from_directory("pdfs/pdf/", filename=download_file.file_location_name, as_attachment=True)
except FileNotFoundError:
abort(404)
@app.route(“/pdf/download/”,方法=['GET','POST'])
def下载(pdf_id):
当前ip=SomeIP地址
req=请求。请求(“获取”http://anotherwebsite.com/json/someIPaddress?fields=169')
req_two=req.json()
状态=请求二[“状态”]
国家=需要两个['country']
城市=需要两个城市
下载文件=Pdf\u info.query.get(Pdf\u id)
如果Ipaddress.query.filter(Ipaddress.ip\u address==current\u ip,Ipaddress.pdfid==pdf\u id)。first():
尝试:
从目录返回send\u(“pdfs/pdf/”,filename=download\u file.file\u location\u name,as\u attachment=True)
除FileNotFoundError外:
中止(404)
其他:
如果状态==“成功”:
ip\u adding=Ipaddress(ip\u address=current\u ip,pdfid=pdf\u id,宕机时间=datetime.utcnow(),国家=国家,地点=城市)
db.session.add(ip_添加)
db.session.commit()
尝试:
从目录返回send\u(“pdfs/pdf/”,filename=download\u file.file\u location\u name,as\u attachment=True)
除FileNotFoundError外:
中止(404)
其他:
ip\u adding=Ipaddress(ip\u address=current\u ip,pdfid=pdf\u id,宕机时间=datetime.utcnow())
db.session.add(ip_添加)
db.session.commit()
尝试:
从目录返回send\u(“pdfs/pdf/”,filename=download\u file.file\u location\u name,as\u attachment=True)
除FileNotFoundError外:
中止(404)
有人能解释一下为什么这不起作用,或者提出一个解决方案吗?您正在尝试获取:
country = req_two['country']
city = req_two['city']
在测试输出之前:
status = req_two['status']
因此,如果status
为fail
,则country=
和city=
将失败
使用:
如果找不到键而不是``KeyError
,则将返回None
。它还允许您在事后测试
country和
city`变量。非常感谢@Adrian Klaver,您的解决方案成功了。
country = req_two.get('country')
city = req_two.get('city')