python - 机器人框架: send binary data in POST request body with

标签 python python-requests http-post robotframework

我在使用 Robot Framework 和 robotframework-requests 运行测试时遇到问题。我需要发送一个 POST 请求和正文中的二进制数据。我已经看过 this question 了,但还没有真正回答。我的测试用例如下所示:

Upload ${filename} file
    Create Session  mysession     http://${ADDRESS}
    ${data} =   Get Binary File     ${filename}
    &{headers} =    Create Dictionary   Content-Type=application/octet-stream   Accept=application/octet-stream
    ${resp} =   Post Request    mysession     ${CGIPath}  data=${data}    headers=&{headers}
    [Return]    ${resp.status_code}     ${resp.text}

问题是我的二进制数据大约有 250MB。当使用 Get Binary File 读取数据时,我发现内存消耗高达 2.x GB。几秒钟后,当触发 Post Request 时,我的测试被 OOM 终止。我已经查看了 files 参数,但似乎它使用了分段编码上传,这不是我需要的。

我的另一个想法是将打开的文件处理程序直接传递到底层请求库,但我想这需要对 robotsframework-request 进行修改。另一个想法是仅针对此测试回退到curl。

我在测试中遗漏了什么吗?解决这个问题的更好方法是什么?

最佳答案

我继续修改robotframework-request的想法并添加了这个方法

def post_request_binary(                                                                                          
        self,                                                                                                     
        alias,
        uri,
        path=None,
        params=None,
        headers=None,
        allow_redirects=None,                                                                                     
        timeout=None):      

    session = self._cache.switch(alias)                                                                           
    redir = True if allow_redirects is None else allow_redirects                                                  
    self._capture_output()  

    method_name = "post"    
    method = getattr(session, method_name)                                                                        

    with open(path, 'rb') as f:                                                                                   
        resp = method(self._get_url(session, uri),                                                                
                      data=f,                                                                                     
                      params=self._utf8_urlencode(params),                                                        
                      headers=headers,                                                                            
                      allow_redirects=allow_redirects,                                                            
                      timeout=self._get_timeout(timeout),                                                         
                      cookies=self.cookies,                                                                       
                      verify=self.verify)                                                                         

    self._print_debug()                                                                                           

    # Store the last session object                                                                               
    session.last_resp = resp

    self.builtin.log(method_name + ' response: ' + resp.text, 'DEBUG')                                            

    return resp

我想我可以稍微改进一下并创建一个拉取请求。

关于python - 机器人框架: send binary data in POST request body with,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54907281/

相关文章:

python - Google App Engine - 创建文档编辑器/进入 Google 文档?

python - Django 模板 : False vs. 无

python - Pillow 无法为不同字体呈现乌尔都语文本

python - Cloudflare 如何区分 Selenium 和 Requests 流量?

java - HttpClient只会执行一次Android

django - 将额外数据附加到 Django 中的 request.POST

Python多线程程序的最大线程数

python - 如何使用 Python Requests 模块模拟 HTTP 发布请求?

python 请求和 TLS

javax.net.ssl.SSLHandshakeException : Remote host closed connection during handshake exception