第三方RestAPI提供了服务器日志文件,此刻我们正在使用curl命令下载日志文件,例如curl -H "X-Auth-Token: XXXXXXXXXXXXXXXXXXXXXXXXXX" https://host_address/api/v3.0/admin/logs -o logs.zip但是我正在尝试使用Flask / Python创建简单的仪表板,这是我的路线的Python / Flask代码:@app.route('/download/server/logs')def download_log(): import requests from flask import send_file res = requests.get('http://<rest_api_host>/v1.2/admin/logs', stream=True) return send_file( res.content, attachment_filename='console_log.zip', mimetype='application/zip' )但是,当我从浏览器中访问该网址时,出现以下错误,Traceback (most recent call last):......... File "/Users/admin/Documents/project/__init__.py", line 940, in download_console_logs res.content, File "/Users/admin/Documents/project/venv3/lib/python3.6/site-packages/requests/models.py", line 823, in content self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes() File "/Users/admin/Documents/project/venv3/lib/python3.6/site-packages/requests/models.py", line 745, in generate for chunk in self.raw.stream(chunk_size, decode_content=True): File "/Users/admin/Documents/project/venv3/lib/python3.6/site-packages/urllib3/response.py", line 436, in stream data = self.read(amt=amt, decode_content=decode_content) File "/Users/admin/Documents/project/venv3/lib/python3.6/site-packages/urllib3/response.py", line 384, in read data = self._fp.read(amt) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 449, in read n = self.readinto(b) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 497, in readinto self._close_conn() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 403, in _close_conn fp.close()AttributeError: 'NoneType' object has no attribute 'close'我使用PyCharm放置了断点/调试器,并能够看到其中res.content包含二进制数据,但是我无法弄清楚这里出了什么问题。这是简单的图表,解释了我要做什么,
3 回答
慕村225694
TA贡献1880条经验 获得超4个赞
我采用了以下方法,它以非常有效的方式解决了我的问题。
@app.route('/download/server/logs')
def download_log():
import requests
from flask import Reponse
res = requests.get('http://<rest_api_host>/v1.2/admin/logs', stream=True)
return Response(
res.iter_content(chunk_size=1024),
direct_passthrough=True
)
添加回答
举报
0/150
提交
取消