带有Flask的JSON API-500内部服务器错误

时间:2019-08-01 06:45:41

标签: python api flask server json-api

我绝对是Flask和API方面的新手。我想用FLASK在本地主机中托管带有API的Json。但它说

  

500内部服务器错误。

在我第一次尝试运行此Flask时,它给了我500 Internal Server错误。然后,我从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)

这是错误。日志

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 -

这是错误And this is the error

1 个答案:

答案 0 :(得分:0)

我注意到有一个

  

pymongo.errors.OperationFailure:没有经过身份验证的用户

当我发布此问题时

只需添加此 在Windows

client = MongoClient('mongodb://user:pass@localhost:27017/')

或在Linux中

client = MongoClient('mongodb://user:pass@172.17.0.1:27017/')

我没有删除它,因为其他人可能对我也有同样的问题