JSON API 使用 Flask - 500 内部服务器错误
JSON API with Flask - 500 Internal Server Error
我绝对是 Flask 和 API 方面的新手。我想使用 FLASK 在本地主机中使用 API 托管我的 Json。但它说
500 Internal Server Error.
在我第一次尝试 运行 这个 Flask 时,它给我这个 500 内部服务器错误。然后我从Ubuntu到Windows玩游戏和浏览(我尽量解释清楚)。我又回到了Ubuntu。
我做的事情和我第一次尝试做的完全一样,我得到了这个 500 Internal
服务器错误。但我不知道,这次它起作用了。我敢肯定
我什么都没做,就像我第一次尝试一样
这是代码
from flask import Flask
from flask import request
from pymongo import MongoClient
from bson.json_util import dumps
import json
client = MongoClient()
db = client.dbCrawlTwint
app = Flask(__name__)
@app.route("/get_statistic_year", methods = ['GET'])
def get_statistic_year():
statisticYear = db.statisticYear.find()
return dumps(statisticYear)
@app.route("/get_statistic_month", methods = ['GET'])
def get_statistic_month():
statisticMonth = db.statisticMonth.find()
return dumps(statisticMonth)
@app.route("/get_statistic_week", methods = ['GET'])
def get_statistic_week():
statisticWeek = db.statisticWeek.find()
return dumps(statisticWeek)
@app.route("/get_statistic_day", methods = ['GET'])
def get_statistic_day():
statisticDay = db.statisticDay.find()
return dumps(statisticDay)
这是 error.log
INFO:werkzeug:127.0.0.1 - - [01/Aug/2019 13:21:13] "GET /get_statistic_month HTTP/1.1" 500 -
ERROR:restAPI:Exception on /get_statistic_month [GET]
Traceback (most recent call last):
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/alramdein/Documents/Angular-exp/angular-statistic/flask/restAPI.py", line 21, in get_statistic_month
return dumps(statisticMonth)
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/bson/json_util.py", line 383, in dumps
return json.dumps(_json_convert(obj, json_options), *args, **kwargs)
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/bson/json_util.py", line 421, in _json_convert
return list((_json_convert(v, json_options) for v in obj))
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/bson/json_util.py", line 421, in <genexpr>
return list((_json_convert(v, json_options) for v in obj))
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/pymongo/cursor.py", line 1225, in next
if len(self.__data) or self._refresh():
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/pymongo/cursor.py", line 1140, in _refresh
self.__send_message(q)
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/pymongo/cursor.py", line 1010, in __send_message
helpers._check_command_response(first)
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/pymongo/helpers.py", line 155, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: there are no users authenticated
INFO:werkzeug:127.0.0.1 - - [01/Aug/2019 13:21:14] "GET /get_statistic_month HTTP/1.1" 500 -
这就是错误
我注意到有一个
pymongo.errors.OperationFailure: there are no users authenticated
当我post这个问题
只需添加这个
在 Windows
client = MongoClient('mongodb://user:pass@localhost:27017/')
或 Linux
中的这个
client = MongoClient('mongodb://user:pass@172.17.0.1:27017/')
我没有删除这个,因为其他人可能和我有同样的问题
我绝对是 Flask 和 API 方面的新手。我想使用 FLASK 在本地主机中使用 API 托管我的 Json。但它说
500 Internal Server Error.
在我第一次尝试 运行 这个 Flask 时,它给我这个 500 内部服务器错误。然后我从Ubuntu到Windows玩游戏和浏览(我尽量解释清楚)。我又回到了Ubuntu。
我做的事情和我第一次尝试做的完全一样,我得到了这个 500 Internal 服务器错误。但我不知道,这次它起作用了。我敢肯定 我什么都没做,就像我第一次尝试一样
这是代码
from flask import Flask
from flask import request
from pymongo import MongoClient
from bson.json_util import dumps
import json
client = MongoClient()
db = client.dbCrawlTwint
app = Flask(__name__)
@app.route("/get_statistic_year", methods = ['GET'])
def get_statistic_year():
statisticYear = db.statisticYear.find()
return dumps(statisticYear)
@app.route("/get_statistic_month", methods = ['GET'])
def get_statistic_month():
statisticMonth = db.statisticMonth.find()
return dumps(statisticMonth)
@app.route("/get_statistic_week", methods = ['GET'])
def get_statistic_week():
statisticWeek = db.statisticWeek.find()
return dumps(statisticWeek)
@app.route("/get_statistic_day", methods = ['GET'])
def get_statistic_day():
statisticDay = db.statisticDay.find()
return dumps(statisticDay)
这是 error.log
INFO:werkzeug:127.0.0.1 - - [01/Aug/2019 13:21:13] "GET /get_statistic_month HTTP/1.1" 500 -
ERROR:restAPI:Exception on /get_statistic_month [GET]
Traceback (most recent call last):
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/alramdein/Documents/Angular-exp/angular-statistic/flask/restAPI.py", line 21, in get_statistic_month
return dumps(statisticMonth)
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/bson/json_util.py", line 383, in dumps
return json.dumps(_json_convert(obj, json_options), *args, **kwargs)
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/bson/json_util.py", line 421, in _json_convert
return list((_json_convert(v, json_options) for v in obj))
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/bson/json_util.py", line 421, in <genexpr>
return list((_json_convert(v, json_options) for v in obj))
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/pymongo/cursor.py", line 1225, in next
if len(self.__data) or self._refresh():
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/pymongo/cursor.py", line 1140, in _refresh
self.__send_message(q)
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/pymongo/cursor.py", line 1010, in __send_message
helpers._check_command_response(first)
File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/pymongo/helpers.py", line 155, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: there are no users authenticated
INFO:werkzeug:127.0.0.1 - - [01/Aug/2019 13:21:14] "GET /get_statistic_month HTTP/1.1" 500 -
这就是错误
我注意到有一个
pymongo.errors.OperationFailure: there are no users authenticated
当我post这个问题
只需添加这个 在 Windows
client = MongoClient('mongodb://user:pass@localhost:27017/')
或 Linux
中的这个client = MongoClient('mongodb://user:pass@172.17.0.1:27017/')
我没有删除这个,因为其他人可能和我有同样的问题