python生成post文件请求

2024-04-29 01:57:01 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,伙计们,我正在开发一个python3quartasyncio应用程序,并试图围绕我的httpapi设置一个测试框架。你知道吗

Quart有方法来构建json、表单和原始请求,但没有文件请求。我相信我需要建立自己的请求包和张贴一个“原始”的请求。 使用postman,我可以看到请求需要如下所示:

----------------------------298121837148774387758621\r\n
Content-Disposition: form-data; name="firmware"; filename="image.bin"\r\n
Content-Type: application/octet-stream\r\n
\r\n
\x00@\x00\x10\x91\xa0\t\x08+\xaa\t\x08/\xaa\t\x083\xaa\t\x087\xaa\t\x08;\xaa\t\x08\x00\x00\x00\
....
\xff\xff\xff\xff\xff\xff\xff\xa5\t\tZ\x0c\x00Rotea MLU Main V0.12\x00\x00k%\xea\x06\r\n
----------------------------298121837148774387758621--\r\n

如果有一个方法存在的话,我不希望自己对它进行编码。你知道吗

Python中是否有一个模块,我可以在其中构建原始数据包数据并用quartapi发送它?你知道吗

我尝试过使用夸脱请求:

    import requests
    from .web_server import app as quart_app

    test_client = quart_app.test_client()
    firmware_image = 'test.bin'
    with open(firmware_image, 'rb') as f:
        data = f.read()
    files = {'firmware': (firmware_image, data , 'application/octet-stream')}
    firmware_req = requests.Request('POST', 'http://localhost:5000/firmware_update', files=files).prepare()
    response = await test_client.post('/firmware_update',
                                      data=firmware_req.body,
                                      headers={'Content-type': 'multipart/form-data'})

如有任何建议,将不胜感激。你知道吗

干杯。米奇。你知道吗


Tags: 方法testimageformclientappdatabin