【问题标题】:How can I download a binary file with urequests in MicroPython?如何在 MicroPython 中下载带有 urequests 的二进制文件?
【发布时间】:2021-10-15 03:20:08
【问题描述】:

我正在尝试通过基本身份验证的 urequests 下载小于 1 MB 的二进制文件。身份验证部分有效,我得到了预期的响应。

但事实是我感到迷茫,因为我无法下载我需要的文件,我必须通过 urequests 来完成。

你能帮帮我吗?

website = 'www.example.com/api'
username = 'test'
password = 'test'
auth_str = '%s:%s' % (username, password)
b64_auth_str = b2a_base64(auth_str)
headers = {'Authorization': 'Basic %s' % b64_auth_str.decode('utf-8')}
r = urequests.get(website,  headers=headers)

编辑:

由于urequests库的“限制”,我试过这个:mrequests

最佳库:https://github.com/SpotlightKid/mrequests

 import mrequests
 username = 'test'
 password = 'test'
 auth_str = '%s:%s' % (username, password)
 b64_auth_str = b2a_base64(auth_str)
 headers = {'Authorization': 'Basic %s' % b64_auth_str.decode('utf-8'),'accept': 'multipart/form-data'}
    url = "https://domainexample.com/file.bin"
 r = mrequests.get(url, headers=headers)
 r.save("file.bin",1024)
 r.close()

我最终编辑了 Save 方法来处理所需文件的下载百分比。这不是强制性的,但进度条总是健康的;-)

【问题讨论】:

    标签: micropython


    【解决方案1】:

    我不确定 urequests 是否可以处理填充。我知道 json 和 text 效果很好

    r = urequests.get(website,  headers=headers)
    data = r.text  # if text
    data= r.json() # if json
    r.close
    
    # if you look at the type of data and it is a form of json response
    # you will see its just a list of dict in python
    print(type(ifJsonData))
    

    但是,如果二进制文件没有扩展名(.hex),我认为你不需要做任何事情。

    r = urequests.get(website, headers=headers)
    data = r.text  # if text
    r.close
    print("data")
    

    响应将是: b'I was the binary data you received'

    b 表示它是二进制并被翻译成 ASCII(据我所知)。

    【讨论】:

    • 处理部分会丢失,限制chunk,否则会溢出micro的内存。
    猜你喜欢
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 2014-11-13
    • 2022-01-16
    相关资源
    最近更新 更多