我正在尝试编写一个 python 脚本来从 Synology NAS API 下载文件
使用的网址:
http://IP_ADDRESS:PORT/webapi/entry.cgi?api=SYNO.FileStation.Download&version=2&method=download&path=%2FPATH%2FTO%2FFILE&mode=download&sid=SID
从浏览器下载文件是可以的,所以带 GET 的 URL 是有效的,使用 python 我只能得到一个包含这个内容的文件,即使请求的 status_code 是 200 OK:
{"error":{"code":119},"success":false}
在python中尝试过的方法:
download = requests.get(download_url, allow_redirects=True)
open('file.csv', 'wb').write(download.content)
和:
x = requests.get(download_url, stream = True)
with open("file.csv", "wb") as csv:
for chunk in x.iter_content(chunk_size=1024):
if chunk:
csv.write(chunk)
没有成功
最佳答案
当使用从 Synology header 下载文件的请求时,应包括 Content-Type 和 Content-Disposition:
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
'Content-Type': 'application/octet-stream',
'Content-Disposition': 'attachment'
}
然后将 _sid 附加到 URI:
/webapi/entry.cgi?api=SYNO.FileStation.Download&version=2&method=download&path=/sample.xlsx&mode=open&sid=yss5QiB17T8NI1940PEN104300&&_sid=yss5QiB17T8NI1940PEN104300
当然,sid是可变的。
关于python 从 Synology FileStation API 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62696185/