正在从 slack url 下载 encrpyted/compressed 7z 文件

Downloading encrpyted/compressed 7z file from slack url

尝试下载 encrypted/compressed 7zip 文件时遇到问题。我看到正在返回二进制数据,但仍然收到文件未下载的错误。 'url_private' 属性 从 slack api 中获取信息。身份验证很好,我在记录器中看到了字节数据,但仍有问题。

import requests
import py7zr

url= "https://someurl/test_file.7z"
headers = {"auth": "token"}
response = requests.request("GET", url=file_url, headers=headers)
file_name = file_url.split("/")[-1]
if response.status_code == 200:
        with py7zr.SevenZipFile(file_name, mode='r', password=pw) as file:
            file.extractall(f"templates/files/")
            # logger.debug(file_data)
            file.close()

错误:

Failed to run listener function (error: [Errno 2] No such file or directory: 'test_file.7z')
Traceback (most recent call last):
  File "/opt/venv/lib/python3.8/site-packages/slack_bolt/listener/thread_runner.py", line 103, in run_ack_function_asynchronously
    listener.run_ack_function(request=request, response=response)
  File "/opt/venv/lib/python3.8/site-packages/slack_bolt/listener/custom_listener.py", line 49, in run_ack_function
    return self.ack_function(
  File "/app/app/routes/events.py", line 62, in handle_file_shared
    file = get_file_shared(file_url)
  File "/app/app/lib/file_shared.py", line 28, in get_file_shared
    with py7zr.SevenZipFile(file_name, mode='r', password=pw) as file:
  File "/opt/venv/lib/python3.8/site-packages/py7zr/py7zr.py", line 324, in __init__
    self.fp = open(file, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'test_file.7z'

我似乎找不到解决方案,感谢任何帮助。

你大部分都答对了。您的问题之一可能是您没有像请求那样导入包。我通过简单的 google 搜索找到了它。

import os
import requests
import py7zr

URL = "https://dl-alt1.winworldpc.com/Microsoft%20Windows%201.01%20Demo%20Slideshow%20(5.25).7z"
filename = os.path.basename(URL)

response = requests.get(URL, stream=True)

if response.status_code == 200:
    with open(filename, 'wb') as out:
        out.write(response.content)
    with py7zr.SevenZipFile(filename, 'r') as archive:
        archive.extractall(f"templates/files/")
else:
    print('Request failed: %d' % response.status_code)