简体   繁体   English

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

[英]JSON API with Flask - 500 Internal Server Error

I'm an absolute newbie on Flask and API things. 我绝对是Flask和API方面的新手。 I want to host my Json with API in localhost with FLASK. 我想用FLASK在本地主机中托管带有API的Json。 But it says 但是它说

500 Internal Server Error. 500内部服务器错误。

At my first try to run this Flask, it give me this 500 Internal Server error. 在我第一次尝试运行此Flask时,它给了我500内部服务器错误。 Then I go from Ubuntu to Windows to play a game and browsing (I try to explain it clearly). 然后,我从Ubuntu转到Windows,玩游戏并浏览(我试图解释清楚)。 And I come back again to Ubuntu. 然后我再次回到Ubuntu。

And I'm doing the exactly same thing as I'm doing it in my first try which I got this 500 Internal Server error. 而且,我做的事情与第一次尝试完全相同,我遇到了500个内部服务器错误。 BUT I DON'T KNOW, this time it works. 但是我不知道,这次可以了。 And I'm sure that I don't do anything but just like my first try 而且我敢肯定,除了像我第一次尝试一样,我什么也不会做

This is the code 这是代码

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)

And this is the error.log 这是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 -

And this is the error 这是错误 这是错误

I'm notice that there is a 我注意到有一个

pymongo.errors.OperationFailure: there are no users authenticated pymongo.errors.OperationFailure:没有经过身份验证的用户

When I post this question 当我发布这个问题时

Just add this in Windows 只需在Windows中添加

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

or this in Linux 或在Linux中

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

I didn't delete this because other people might have the same problem with me 我之所以没有删除它,是因为其他人可能也遇到同样的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM