【问题标题】:Flask API - 500 Internal Server Error Status CodeFlask API - 500 内部服务器错误状态代码
【发布时间】:2021-10-28 13:07:07
【问题描述】:

我正在 Flask 中构建一个 API,以从新闻网站的不同 RSS 提要中获取新闻,我得到了大部分结果。但是,有时我会随机得到一个 500 Internal Server Error 状态码,并在控制台中记录下来:

[2021-08-29 16:49:40,852] ERROR in app: Exception on /world [GET]
Traceback (most recent call last):
  File "/Users/ragz/Library/Python/3.8/lib/python/site-packages/flask/app.py", line 1513, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/ragz/Library/Python/3.8/lib/python/site-packages/flask/app.py", line 1499, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/Users/ragz/Library/Python/3.8/lib/python/site-packages/flask_restful/__init__.py", line 467, in wrapper
    resp = resource(*args, **kwargs)
  File "/Users/ragz/Library/Python/3.8/lib/python/site-packages/flask/views.py", line 83, in view
    return self.dispatch_request(*args, **kwargs)
  File "/Users/ragz/Library/Python/3.8/lib/python/site-packages/flask_restful/__init__.py", line 582, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/Users/ragz/dev/Python/news_summary/src/python/api.py", line 14, in get
    worldnews = feedparser.parse(random.choice(list(source.world_news)))
  File "/Users/ragz/Library/Python/3.8/lib/python/site-packages/feedparser/api.py", line 216, in parse
    data = _open_resource(url_file_stream_or_string, etag, modified, agent, referrer, handlers, request_headers, result)
  File "/Users/ragz/Library/Python/3.8/lib/python/site-packages/feedparser/api.py", line 115, in _open_resource
    return http.get(url_file_stream_or_string, etag, modified, agent, referrer, handlers, request_headers, result)
  File "/Users/ragz/Library/Python/3.8/lib/python/site-packages/feedparser/http.py", line 172, in get
    data = f.read()
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py", line 471, in read
    s = self._safe_read(self.length)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py", line 614, in _safe_read
    raise IncompleteRead(data, amt-len(data))
http.client.IncompleteRead: IncompleteRead(1606 bytes read, 2306 more expected)

我查看了其他一些 Stack Overflow 答案,但没有找到很多相关的... 有谁知道这里的错误是什么?

这是我的代码 -

import feedparser
from flask import Flask
from flask_restful import Resource, Api, reqparse
import random
import source
import requests

app = Flask(__name__)
api = Api(app)

class WorldNews(Resource):
    # methods go here
    def get(self):
        worldnews = feedparser.parse(random.choice(list(source.world_news)))
        entry = random.choice(list(worldnews.entries))
        title = entry.title # convert dataframe to dictionary
        summary = entry.summary 
        date = entry.published 
        link = entry.link 
        return {'title': title, 'summary': summary, 'date': date, 'link': link, }, 200  # return data and 200 OK code
    pass

api.add_resource(WorldNews, '/world')  # '/users' is our entry point

class TechNews(Resource):
    # methods go here
    def get(self):
        technews = feedparser.parse(random.choice(list(source.tech_sources)))
        entry = random.choice(list(technews.entries))
        title = entry.title # convert dataframe to dictionary
        summary = entry.summary 
        date = entry.published 
        link = entry.link 
        return {'title': title, 'summary': summary, 'date': date, 'link': link, }, 200  # return data and 200 OK code
    pass

api.add_resource(TechNews, '/tech')

class BusinessNews(Resource):
    # methods go here
    def get(self):
        technews = feedparser.parse(random.choice(list(source.business)))
        entry = random.choice(list(technews.entries))
        title = entry.title # convert dataframe to dictionary
        summary = entry.summary 
        date = entry.published 
        link = entry.link 
        return {'title': title, 'summary': summary, 'date': date, 'link': link, }, 200  # return data and 200 OK code
    pass

api.add_resource(BusinessNews, '/business')

class SportsNews(Resource):
    # methods go here
    def get(self):
        technews = feedparser.parse(random.choice(list(source.sports)))
        entry = random.choice(list(technews.entries))
        title = entry.title # convert dataframe to dictionary
        summary = entry.summary 
        date = entry.published 
        link = entry.link 
        return {'title': title, 'summary': summary, 'date': date, 'link': link, }, 200  # return data and 200 OK code
    pass

api.add_resource(SportsNews, '/sports')

class ScienceNews(Resource):
    # methods go here
    def get(self):
        technews = feedparser.parse(random.choice(list(source.science)))
        entry = random.choice(list(technews.entries))
        title = entry.title # convert dataframe to dictionary
        summary = entry.summary 
        date = entry.published 
        link = entry.link 
        return {'title': title, 'summary': summary, 'date': date, 'link': link, }, 200  # return data and 200 OK code
    pass

api.add_resource(ScienceNews, '/science')

class HealthNews(Resource):
    # methods go here
    def get(self):
        technews = feedparser.parse(random.choice(list(source.health)))
        entry = random.choice(list(technews.entries))
        title = entry.title # convert dataframe to dictionary
        summary = entry.summary 
        date = entry.published 
        link = entry.link 
        return {'title': title, 'summary': summary, 'date': date, 'link': link, }, 200  # return data and 200 OK code
    pass

api.add_resource(HealthNews, '/health')

class EntertainmentNews(Resource):
    # methods go here
    def get(self):
        technews = feedparser.parse(random.choice(list(source.entertainment)))
        entry = random.choice(list(technews.entries))
        title = entry.title # convert dataframe to dictionary
        summary = entry.summary 
        date = entry.published 
        link = entry.link 
        return {'title': title, 'summary': summary, 'date': date, 'link': link, }, 200  # return data and 200 OK code
    pass

api.add_resource(EntertainmentNews, '/entertainment')




if __name__ == '__main__':
    app.run()  

我还有一本字典,每个类别都有不同的 RSS 提要。

【问题讨论】:

    标签: python api flask http-status-code-500


    【解决方案1】:

    我前段时间构建了一个功能与此类似的 webapp,但由于连接中的数据丢失而出现此错误,即feedparser 尝试解析提要,但连接在解析数据的过程中中断,因此读取不完整错误。

    看起来你可以用另一个来源替换它,因为你正在阅读随机网站,所以我建议你做这样的事情并记录导致这个错误的网站,看看是否有任何屡犯者,然后将它们从你的如果是这样的话,来源。

    所以:

    try:
        randomly_chosen_news_sources = random.choices(tuple(set((source.world_news))), k=2)
        first_one_to_try = randomly_chosen_news_sources[0]
        backup = randomly_chosen_news_sources[1]
        worldnews = feedparser.parse(randomly_chosen_news_source)
    except Exception as e:
        print(e)  # do this first to figure out what the error message is that comes up then replace e with appropriate error
        # then log the results
        app.logger.error("Connection error while parsing feed {}".format(randomly_chosen_news_source))
        worldnews = feedparser.parse(backup)
    

    当然,这样做的风险是您的备份选择可能会导致相同的错误。如果这是一个问题,我会将 try/except 逻辑提取到它自己的方法中,然后在每次对源进行采样时应用它。

    也许更好的方法是这样做 - 我还使用 tenacity. 在我的应用程序中构建了更多弹性

    这样的事情应该可以解决问题:

    from tenacity import retry, stop_after_attempt
    
    
    @retry(stop=stop_after_attempt(5))
    def get(self):
    

    【讨论】:

    • 哇,谢谢!坚韧的方法对我帮助很大。我试图使用与此类似的逻辑来解决这个问题,但我无法让它发挥作用,而且我不知道这个插件存在!