<p dir="auto">Tried this code and it works flawless with files &lt; 2GB but as soon as I'm using files &gt; 2GB it gives me an error.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="e = MultipartEncoder(fields=dict(
            file=(rfbase, open(filename, 'rb'), 'text/plain')
        ))
 m = MultipartEncoderMonitor(e, my_callback)
uploadInfo = requests.request('POST', uploadURL,data=m,headers={'Content-Type': m.content_type})

It gives me the following error:
  File &quot;D:/Upload-Tool/req.py&quot;, line 86, in upload
    uploadInfo = requests.request(&quot;POST&quot;, uploadURL,data=m,headers={'Content-Type': m.content_type})
  File &quot;C:\Python27\lib\site-packages\requests\api.py&quot;, line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File &quot;C:\Python27\lib\site-packages\requests\sessions.py&quot;, line 444, in request
    data = data or {},
  File &quot;C:\Python27\lib\site-packages\requests_toolbelt\multipart\encoder.py&quot;, line 292, in __len__
    return len(self.encoder)
  File &quot;C:\Python27\lib\site-packages\requests_toolbelt\multipart\encoder.py&quot;, line 96, in __len__
    return self._len or self._calculate_length()
  File &quot;C:\Python27\lib\site-packages\requests_toolbelt\multipart\encoder.py&quot;, line 111, in _calculate_length
    ) + boundary_len + 4
  File &quot;C:\Python27\lib\site-packages\requests_toolbelt\multipart\encoder.py&quot;, line 110, in &lt;genexpr&gt;
    (boundary_len + len(p) + 4) for p in self.parts
  File &quot;C:\Python27\lib\site-packages\requests_toolbelt\multipart\encoder.py&quot;, line 377, in __len__
    return len(self.headers) + super_len(self.body)
  File &quot;C:\Python27\lib\site-packages\requests\utils.py&quot;, line 52, in super_len
    return len(o)
OverflowError: long int too large to convert to int"><pre class="notranslate"><span class="pl-s1">e</span> <span class="pl-c1">=</span> <span class="pl-v">MultipartEncoder</span>(<span class="pl-s1">fields</span><span class="pl-c1">=</span><span class="pl-en">dict</span>(
            <span class="pl-s1">file</span><span class="pl-c1">=</span>(<span class="pl-s1">rfbase</span>, <span class="pl-en">open</span>(<span class="pl-s1">filename</span>, <span class="pl-s">'rb'</span>), <span class="pl-s">'text/plain'</span>)
        ))
 <span class="pl-s1">m</span> <span class="pl-c1">=</span> <span class="pl-v">MultipartEncoderMonitor</span>(<span class="pl-s1">e</span>, <span class="pl-s1">my_callback</span>)
<span class="pl-s1">uploadInfo</span> <span class="pl-c1">=</span> <span class="pl-s1">requests</span>.<span class="pl-en">request</span>(<span class="pl-s">'POST'</span>, <span class="pl-s1">uploadURL</span>,<span class="pl-s1">data</span><span class="pl-c1">=</span><span class="pl-s1">m</span>,<span class="pl-s1">headers</span><span class="pl-c1">=</span>{<span class="pl-s">'Content-Type'</span>: <span class="pl-s1">m</span>.<span class="pl-s1">content_type</span>})

<span class="pl-v">It</span> <span class="pl-s1">gives</span> <span class="pl-s1">me</span> <span class="pl-s1">the</span> <span class="pl-s1">following</span> <span class="pl-s1">error</span>:
  <span class="pl-v">File</span> <span class="pl-s">"D:/Upload-Tool/req.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">86</span>, <span class="pl-c1">in</span> <span class="pl-s1">upload</span>
    <span class="pl-s1">uploadInfo</span> <span class="pl-c1">=</span> <span class="pl-s1">requests</span>.<span class="pl-en">request</span>(<span class="pl-s">"POST"</span>, <span class="pl-s1">uploadURL</span>,<span class="pl-s1">data</span><span class="pl-c1">=</span><span class="pl-s1">m</span>,<span class="pl-s1">headers</span><span class="pl-c1">=</span>{<span class="pl-s">'Content-Type'</span>: <span class="pl-s1">m</span>.<span class="pl-s1">content_type</span>})
  <span class="pl-v">File</span> <span class="pl-s">"C:\Python27\lib\site-packages<span class="pl-cce">\r</span>equests<span class="pl-cce">\a</span>pi.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">50</span>, <span class="pl-s1">in</span> <span class="pl-s1">request</span>
    <span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">session</span>.<span class="pl-en">request</span>(<span class="pl-s1">method</span><span class="pl-c1">=</span><span class="pl-s1">method</span>, <span class="pl-s1">url</span><span class="pl-c1">=</span><span class="pl-s1">url</span>, <span class="pl-c1">**</span><span class="pl-s1">kwargs</span>)
  <span class="pl-v">File</span> <span class="pl-s">"C:\Python27\lib\site-packages<span class="pl-cce">\r</span>equests\sessions.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">444</span>, <span class="pl-s1">in</span> <span class="pl-s1">request</span>
    <span class="pl-s1">data</span> <span class="pl-c1">=</span> <span class="pl-s1">data</span> <span class="pl-c1">or</span> {},
  <span class="pl-v">File</span> <span class="pl-s">"C:\Python27\lib\site-packages<span class="pl-cce">\r</span>equests_toolbelt\multipart\encoder.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">292</span>, <span class="pl-s1">in</span> <span class="pl-s1">__len__</span>
    <span class="pl-k">return</span> <span class="pl-en">len</span>(<span class="pl-s1">self</span>.<span class="pl-s1">encoder</span>)
  <span class="pl-v">File</span> <span class="pl-s">"C:\Python27\lib\site-packages<span class="pl-cce">\r</span>equests_toolbelt\multipart\encoder.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">96</span>, <span class="pl-s1">in</span> <span class="pl-s1">__len__</span>
    <span class="pl-k">return</span> <span class="pl-s1">self</span>.<span class="pl-s1">_len</span> <span class="pl-c1">or</span> <span class="pl-s1">self</span>.<span class="pl-en">_calculate_length</span>()
  <span class="pl-v">File</span> <span class="pl-s">"C:\Python27\lib\site-packages<span class="pl-cce">\r</span>equests_toolbelt\multipart\encoder.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">111</span>, <span class="pl-s1">in</span> <span class="pl-s1">_calculate_length</span>
    ) <span class="pl-c1">+</span> <span class="pl-s1">boundary_len</span> <span class="pl-c1">+</span> <span class="pl-c1">4</span>
  <span class="pl-v">File</span> <span class="pl-s">"C:\Python27\lib\site-packages<span class="pl-cce">\r</span>equests_toolbelt\multipart\encoder.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">110</span>, <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">genexpr</span><span class="pl-c1">&gt;</span>
    (<span class="pl-s1">boundary_len</span> <span class="pl-c1">+</span> <span class="pl-en">len</span>(<span class="pl-s1">p</span>) <span class="pl-c1">+</span> <span class="pl-c1">4</span>) <span class="pl-k">for</span> <span class="pl-s1">p</span> <span class="pl-c1">in</span> <span class="pl-s1">self</span>.<span class="pl-s1">parts</span>
  <span class="pl-v">File</span> <span class="pl-s">"C:\Python27\lib\site-packages<span class="pl-cce">\r</span>equests_toolbelt\multipart\encoder.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">377</span>, <span class="pl-s1">in</span> <span class="pl-s1">__len__</span>
    <span class="pl-s1">return</span> <span class="pl-en">len</span>(<span class="pl-s1">self</span>.<span class="pl-s1">headers</span>) <span class="pl-c1">+</span> <span class="pl-en">super_len</span>(<span class="pl-s1">self</span>.<span class="pl-s1">body</span>)
  <span class="pl-v">File</span> <span class="pl-s">"C:\Python27\lib\site-packages<span class="pl-cce">\r</span>equests\utils.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">52</span>, <span class="pl-s1">in</span> <span class="pl-s1">super_len</span>
    <span class="pl-s1">return</span> <span class="pl-en">len</span>(<span class="pl-s1">o</span>)
<span class="pl-v">OverflowError</span>: <span class="pl-s1">long</span> <span class="pl-s1">int</span> <span class="pl-s1">too</span> <span class="pl-s1">large</span> <span class="pl-s1">to</span> <span class="pl-s1">convert</span> <span class="pl-s1">to</span> <span class="pl-s1">int</span></pre></div>
<p dir="auto">Please help if my code has some errors :)</p>
<p dir="auto">edit: sorry, totally forgot to upgrade the toolbelt :) thx. a lot</p>
<p dir="auto">Using Python 3.4.0 and Requests 2.6.0</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="    content = MultipartEncoder(fields=[('metadata', (&quot;&quot;, json.dumps(metadata))), ('content', (file_name, open(file_path, 'rb'), mime_type))])"><pre class="notranslate">    <span class="pl-s1">content</span> <span class="pl-c1">=</span> <span class="pl-v">MultipartEncoder</span>(<span class="pl-s1">fields</span><span class="pl-c1">=</span>[(<span class="pl-s">'metadata'</span>, (<span class="pl-s">""</span>, <span class="pl-s1">json</span>.<span class="pl-en">dumps</span>(<span class="pl-s1">metadata</span>))), (<span class="pl-s">'content'</span>, (<span class="pl-s1">file_name</span>, <span class="pl-en">open</span>(<span class="pl-s1">file_path</span>, <span class="pl-s">'rb'</span>), <span class="pl-s1">mime_type</span>))])</pre></div>
<p dir="auto">Then, I call requests.post and get this error.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="File &quot;/home/xxxx/upload.py&quot;, line 222, in upload
    r = requests.post(url, headers=headers, data=content)
  File &quot;/home/xxxx/upload/venv/lib/python3.4/site-packages/requests/api.py&quot;, line 108, in post
    return request('post', url, data=data, json=json, **kwargs)
  File &quot;/home/xxxx/upload/venv/lib/python3.4/site-packages/requests/api.py&quot;, line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File &quot;/home/xxxx/upload/venv/lib/python3.4/site-packages/requests/sessions.py&quot;, line 443, in request
    data = data or {},
  File &quot;/home/xxxx/upload/venv/lib/python3.4/site-packages/requests_toolbelt/multipart/encoder.py&quot;, line 96, in __len__
    return self._len or self._calculate_length()
  File &quot;/home/xxxx/upload/venv/lib/python3.4/site-packages/requests_toolbelt/multipart/encoder.py&quot;, line 111, in _calculate_length
    ) + boundary_len + 4
  File &quot;/home/xxxx/upload/venv/lib/python3.4/site-packages/requests_toolbelt/multipart/encoder.py&quot;, line 110, in &lt;genexpr&gt;
    (boundary_len + len(p) + 4) for p in self.parts
  File &quot;/home/xxxx/upload/venv/lib/python3.4/site-packages/requests_toolbelt/multipart/encoder.py&quot;, line 377, in __len__
    return len(self.headers) + super_len(self.body)
  File &quot;/home/xxxx/upload/venv/lib/python3.4/site-packages/requests/utils.py&quot;, line 52, in super_len
    return len(o)
OverflowError: cannot fit 'int' into an index-sized integer"><pre class="notranslate"><span class="pl-v">File</span> <span class="pl-s">"/home/xxxx/upload.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">222</span>, <span class="pl-s1">in</span> <span class="pl-s1">upload</span>
    <span class="pl-s1">r</span> <span class="pl-c1">=</span> <span class="pl-s1">requests</span>.<span class="pl-en">post</span>(<span class="pl-s1">url</span>, <span class="pl-s1">headers</span><span class="pl-c1">=</span><span class="pl-s1">headers</span>, <span class="pl-s1">data</span><span class="pl-c1">=</span><span class="pl-s1">content</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/home/xxxx/upload/venv/lib/python3.4/site-packages/requests/api.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">108</span>, <span class="pl-s1">in</span> <span class="pl-s1">post</span>
    <span class="pl-k">return</span> <span class="pl-en">request</span>(<span class="pl-s">'post'</span>, <span class="pl-s1">url</span>, <span class="pl-s1">data</span><span class="pl-c1">=</span><span class="pl-s1">data</span>, <span class="pl-s1">json</span><span class="pl-c1">=</span><span class="pl-s1">json</span>, <span class="pl-c1">**</span><span class="pl-s1">kwargs</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/home/xxxx/upload/venv/lib/python3.4/site-packages/requests/api.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">50</span>, <span class="pl-s1">in</span> <span class="pl-s1">request</span>
    <span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">session</span>.<span class="pl-en">request</span>(<span class="pl-s1">method</span><span class="pl-c1">=</span><span class="pl-s1">method</span>, <span class="pl-s1">url</span><span class="pl-c1">=</span><span class="pl-s1">url</span>, <span class="pl-c1">**</span><span class="pl-s1">kwargs</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/home/xxxx/upload/venv/lib/python3.4/site-packages/requests/sessions.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">443</span>, <span class="pl-s1">in</span> <span class="pl-s1">request</span>
    <span class="pl-s1">data</span> <span class="pl-c1">=</span> <span class="pl-s1">data</span> <span class="pl-c1">or</span> {},
  <span class="pl-v">File</span> <span class="pl-s">"/home/xxxx/upload/venv/lib/python3.4/site-packages/requests_toolbelt/multipart/encoder.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">96</span>, <span class="pl-s1">in</span> <span class="pl-s1">__len__</span>
    <span class="pl-k">return</span> <span class="pl-s1">self</span>.<span class="pl-s1">_len</span> <span class="pl-c1">or</span> <span class="pl-s1">self</span>.<span class="pl-en">_calculate_length</span>()
  <span class="pl-v">File</span> <span class="pl-s">"/home/xxxx/upload/venv/lib/python3.4/site-packages/requests_toolbelt/multipart/encoder.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">111</span>, <span class="pl-s1">in</span> <span class="pl-s1">_calculate_length</span>
    ) <span class="pl-c1">+</span> <span class="pl-s1">boundary_len</span> <span class="pl-c1">+</span> <span class="pl-c1">4</span>
  <span class="pl-v">File</span> <span class="pl-s">"/home/xxxx/upload/venv/lib/python3.4/site-packages/requests_toolbelt/multipart/encoder.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">110</span>, <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">genexpr</span><span class="pl-c1">&gt;</span>
    (<span class="pl-s1">boundary_len</span> <span class="pl-c1">+</span> <span class="pl-en">len</span>(<span class="pl-s1">p</span>) <span class="pl-c1">+</span> <span class="pl-c1">4</span>) <span class="pl-k">for</span> <span class="pl-s1">p</span> <span class="pl-c1">in</span> <span class="pl-s1">self</span>.<span class="pl-s1">parts</span>
  <span class="pl-v">File</span> <span class="pl-s">"/home/xxxx/upload/venv/lib/python3.4/site-packages/requests_toolbelt/multipart/encoder.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">377</span>, <span class="pl-s1">in</span> <span class="pl-s1">__len__</span>
    <span class="pl-s1">return</span> <span class="pl-en">len</span>(<span class="pl-s1">self</span>.<span class="pl-s1">headers</span>) <span class="pl-c1">+</span> <span class="pl-en">super_len</span>(<span class="pl-s1">self</span>.<span class="pl-s1">body</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/home/xxxx/upload/venv/lib/python3.4/site-packages/requests/utils.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">52</span>, <span class="pl-s1">in</span> <span class="pl-s1">super_len</span>
    <span class="pl-s1">return</span> <span class="pl-en">len</span>(<span class="pl-s1">o</span>)
<span class="pl-v">OverflowError</span>: <span class="pl-s1">cannot</span> <span class="pl-s1">fit</span> <span class="pl-s">'int'</span> <span class="pl-s1">into</span> <span class="pl-s1">an</span> <span class="pl-s1">index</span><span class="pl-c1">-</span><span class="pl-s1">sized</span> <span class="pl-s1">integer</span></pre></div>
<p dir="auto"><code class="notranslate">__len__</code> can't return values larger than a C int.<br>
<a href="http://bugs.python.org/issue21444" rel="nofollow">http://bugs.python.org/issue21444</a></p>