Flask API - 500 内部服务器错误状态代码
Flask API - 500 Internal Server Error Status Code
我正在 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 提要。
我不久前构建了一个功能与此类似的网络应用程序,但由于连接中的数据丢失而出现此错误,即 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):
我正在 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 提要。
我不久前构建了一个功能与此类似的网络应用程序,但由于连接中的数据丢失而出现此错误,即 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):