python - 从 slack url 下载加密/压缩的 7z 文件

标签 python python-requests slack-api 7zip

尝试下载加密/压缩的 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'

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

最佳答案

你说的大部分都是对的。您的问题之一可能是您没有导入请求之类的包。我通过简单的谷歌搜索找到了它。

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)

关于python - 从 slack url 下载加密/压缩的 7z 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71315849/

相关文章:

javascript - 使用 Python 检索 javascript 文件内容

python - Twitter 不再使用请求库 python

python - 如何禁用 Requests 库中的日志消息?

amazon-web-services - AWS lambda : SES not working when Lex Chatbot is published to Slack

php - 如何 "extract"来自 ReactPHP\Promise\Promise 的数据?

python - 模块未找到错误 : No module named 'flask_session'

python - 列表小部件项数据对象上的槽从未被调用(在 PyQt 5.7 中)

python - 在 Pandas 中组合单独的每日 CSV

python - Scrapy 不会回调除默认​​ 'parse' 之外的其他函数

ruby - Slack RTM API 连接到多个服务器