<p dir="auto">I'm behind a proxy at work, and I'm having issues getting requests to use the proxy. urllib2 is able to use it just fine, but requests fails. I've tried both setting an environment variable (both HTTPS_PROXY and https_proxy) and passing in a dict, but neither work.</p>
<p dir="auto">I'm on OSX 10.7.5 using Python 2.7.3 and requests 1.1.0 installed in a virtualenv via pip.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(osx)gfairchild@stueyemac ~&gt; set | grep -i proxy
HTTPS_PROXY=https://proxy.com:8080
https_proxy=https://proxy.com:8080
(osx)gfairchild@stueyemac ~&gt; python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; import urllib2
&gt;&gt;&gt; r = urllib2.urlopen('https://google.com')
&gt;&gt;&gt; print r.read()
&lt;!doctype html&gt;&lt;html itemscope=&quot;itemscope&quot; itemtype=&quot;http://schema.org/WebPage&quot;&gt;&lt;head&gt;&lt;meta content=&quot;Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for.&quot; name=&quot;description&quot;&gt;&lt;meta content=&quot;noodp&quot; name=&quot;robots&quot;&gt;&lt;meta itemprop=&quot;image&quot; content=&quot;/images/google_favicon_128.png&quot;&gt;&lt;title&gt;Google&lt;/title&gt;&lt;script&gt;(function(){
window.google={kEI:&quot;I-gsUdvaLMn-0gHWhIHIAg&quot;,getEI:function(a){for(var b;a&amp;&amp;(!a.getAttribute||!(b=a.getAttribute(&quot;eid&quot;)));)a=a.parentNode;return b||google.kEI},https:function(){return&quot;https:&quot;==window.location.protocol},kEXPI:&quot;17259,18168,39523,4000116,4001569,4001948,4001959,4001975,4002001,4002159,4002562,4002734,4002855,4002858,4003372,4003374,4003387,4003514,4003575,4003638,4003917,4003944,4003982,4004015,4004064,4004074,4004083,4004152,4004181,4004214,4004241,4004276,4004298&quot;,kCSI:{e:&quot;17259,18168,39523,4000116,4001569,4001948,4001959,4001975,4002001,4002159,4002562,4002734,4002855,4002858,4003372,4003374,4003387,4003514,4003575,4003638,4003917,4003944,4003982,4004015,4004064,4004074,4004083,4004152,4004181,4004214,4004241,4004276,4004298&quot;,ei:&quot;I-gsUdvaLMn-0gHWhIHIAg&quot;},authuser:0,ml:function(){},kHL:&quot;en&quot;,time:function(){return(new Date).getTime()},log:function(a,
b,c,k){var d=new Image,f=google.lc,e=google.li,g=&quot;&quot;;d.onerror=d.onload=d.onabort=function(){delete f[e]};f[e]=d;!c&amp;&amp;-1==b.search(&quot;&amp;ei=&quot;)&amp;&amp;(g=&quot;&amp;ei=&quot;+google.getEI(k));c=c||&quot;/gen_204?atyp=i&amp;ct=&quot;+a+&quot;&amp;cad=&quot;+b+g+&quot;&amp;zx=&quot;+google.time();a=/^http:/i;a.test(c)&amp;&amp;google.https()?(google.ml(Error(&quot;GLMM&quot;),!1,{src:c}),delete f[e]):(d.src=c,google.li=e+1)},lc:[],li:0,Toolbelt:{},y:{},x:function(a,b){google.y[a.id]=[a,b];return!1},load:function(a,b){google.x({id:&quot;l&quot;+a},function(){google.load(a,b)})}};
})();
(function(){var d=!1;google.sn=&quot;webhp&quot;;google.timers={};google.startTick=function(a,b){google.timers[a]={t:{start:google.time()},bfr:!!b}};google.tick=function(a,b,h){google.timers[a]||google.startTick(a);google.timers[a].t[b]=h||google.time()};google.startTick(&quot;load&quot;,!0);
try{}catch(e){}})();
var _gjwl=location;function _gjuc(){var a=_gjwl.href.indexOf(&quot;#&quot;);if(0&lt;=a&amp;&amp;(a=_gjwl.href.substring(a),0&lt;a.indexOf(&quot;&amp;q=&quot;)||0&lt;=a.indexOf(&quot;#q=&quot;)))if(a=a.substring(1),-1==a.indexOf(&quot;#&quot;)){for(var d=0;d&lt;a.length;){var b=d;&quot;&amp;&quot;==a.charAt(b)&amp;&amp;++b;var c=a.indexOf(&quot;&amp;&quot;,b);-1==c&amp;&amp;(c=a.length);b=a.substring(b,c);if(0==b.indexOf(&quot;fp=&quot;))a=a.substring(0,d)+a.substring(c,a.length),c=d;else if(&quot;cad=h&quot;==b)return 0;d=c}_gjwl.href=&quot;/search?&quot;+a+&quot;&amp;cad=h&quot;;return 1}return 0}
function _gjp(){(!window._gjwl.hash||!window._gjuc())&amp;&amp;setTimeout(_gjp,500)};
window._gjp&amp;&amp;_gjp();&lt;/script&gt;&lt;style&gt;#gbar,#guser{font-size:13px;padding-top:1px !important;}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22;margin-right:.5em;vertical-align:top}#gbar{float:left}}a.gb1,a.gb4{text-decoration:underline !important}a.gb1,a.gb4{color:#00c !important}.gbi .gb4{color:#dd8e27 !important}.gbf .gb4{color:#900 !important}&lt;/style&gt;&lt;style&gt;.h{font-family:arial,sans-serif}body{font-family:arial,sans-serif}td{font-family:arial,sans-serif}a{font-family:arial,sans-serif}p{font-family:arial,sans-serif}body{margin:0;overflow-y:scroll}#gog{padding:3px 8px 0}.h{color:#36c}.q{color:#00c}.ts{border-collapse:collapse}td{line-height:.8em}.gac_m td{line-height:17px}form{margin-bottom:20px}.ts td{padding:0}em{font-weight:bold;font-style:normal}.lst{height:25px;width:496px;font:18px arial,sans-serif}.gsfi{font:18px arial,sans-serif}.gsfs{font:17px arial,sans-serif}.ds{display:inline-box;display: inline-block;margin:3px 0 4px;margin-left:4px}input{font-family:inherit}body{background:#fff;color:black}a.gb1{color:#11c !important}a.gb2{color:#11c !important}a.gb3{color:#11c !important}a.gb4{color:#11c !important}.sblc{padding-top:5px}.lsbb{background:#eee;border:solid 1px;border-color:#ccc #999 #999 #ccc;height:30px}a{color:#11c;text-decoration:none}a:hover{text-decoration:underline}a:active{text-decoration:underline}.fl a{color:#36c}a:visited{color:#551a8b}a.gb1{text-decoration:underline}a.gb4{text-decoration:underline}a.gb3:hover{text-decoration:none}.sblc a{display:block;margin:2px 0;margin-left:13px;font-size:11px}#ghead a.gb2:hover{color:#fff !important}.lsbb{display:block}.ftl{display:inline-block;margin:0 12px}.lsb{background:url(/images/srpr/nav_logo80.png) 0 -258px repeat-x;border:none;color:#000;cursor:pointer;height:30px;margin:0;outline:0;font:15px arial,sans-serif;vertical-align:top}#fll a{display:inline-block;margin:0 12px}.lsb:active{background:#ccc}.lst:focus{outline:none}#addlang a{padding:0 3px}&lt;/style&gt;&lt;script&gt;&lt;/script&gt; &lt;/head&gt;&lt;body dir=&quot;ltr&quot; bgcolor=&quot;#fff&quot;&gt;&lt;script&gt;(function(){var src='/images/srpr/nav_logo80.png';var iesg=false;document.body.onload = function(){window.n &amp;&amp; window.n();if (document.images){new Image().src=src;}
if (!iesg){document.f&amp;&amp;document.f.q.focus();document.gbqf&amp;&amp;document.gbqf.q.focus();}
}
})();&lt;/script&gt;&lt;textarea id=&quot;csi&quot; style=&quot;display:none&quot;&gt;&lt;/textarea&gt;&lt;div id=&quot;mngb&quot;&gt;&lt;div id=gbar&gt;&lt;nobr&gt;&lt;b class=gb1&gt;Search&lt;/b&gt; &lt;a class=gb1 href=&quot;https://www.google.com/imghp?hl=en&amp;tab=wi&quot;&gt;Images&lt;/a&gt; &lt;a class=gb1 href=&quot;https://maps.google.com/maps?hl=en&amp;tab=wl&quot;&gt;Maps&lt;/a&gt; &lt;a class=gb1 href=&quot;https://play.google.com/?hl=en&amp;tab=w8&quot;&gt;Play&lt;/a&gt; &lt;a class=gb1 href=&quot;https://www.youtube.com/?tab=w1&quot;&gt;YouTube&lt;/a&gt; &lt;a class=gb1 href=&quot;https://news.google.com/nwshp?hl=en&amp;tab=wn&quot;&gt;News&lt;/a&gt; &lt;a class=gb1 href=&quot;https://mail.google.com/mail/?tab=wm&quot;&gt;Gmail&lt;/a&gt; &lt;a class=gb1 href=&quot;https://drive.google.com/?tab=wo&quot;&gt;Drive&lt;/a&gt; &lt;a class=gb1 style=&quot;text-decoration:none&quot; href=&quot;http://www.google.com/intl/en/options/&quot;&gt;&lt;u&gt;More&lt;/u&gt; &amp;raquo;&lt;/a&gt;&lt;/nobr&gt;&lt;/div&gt;&lt;div id=guser width=100%&gt;&lt;nobr&gt;&lt;span id=gbn class=gbi&gt;&lt;/span&gt;&lt;span id=gbf class=gbf&gt;&lt;/span&gt;&lt;span id=gbe&gt;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/history/optout?hl=en&quot; class=gb4&gt;Web History&lt;/a&gt; | &lt;a  href=&quot;/preferences?hl=en&quot; class=gb4&gt;Settings&lt;/a&gt; | &lt;a target=_top id=gb_70 href=&quot;https://accounts.google.com/ServiceLogin?hl=en&amp;continue=https://www.google.com/&quot; class=gb4&gt;Sign in&lt;/a&gt;&lt;/nobr&gt;&lt;/div&gt;&lt;div class=gbh style=left:0&gt;&lt;/div&gt;&lt;div class=gbh style=right:0&gt;&lt;/div&gt;&lt;/div&gt;&lt;iframe name=&quot;wgjf&quot; style=&quot;display:none&quot;&gt;&lt;/iframe&gt;&lt;center&gt;&lt;br clear=&quot;all&quot; id=&quot;lgpd&quot;&gt;&lt;div id=&quot;lga&quot;&gt;&lt;img alt=&quot;Google&quot; height=&quot;95&quot; src=&quot;/intl/en_ALL/images/srpr/logo1w.png&quot; width=&quot;275&quot; id=&quot;hplogo&quot; onload=&quot;window.lol&amp;&amp;lol()&quot; style=&quot;padding:28px 0 14px&quot;&gt;&lt;br&gt;&lt;br&gt;&lt;/div&gt;&lt;form action=&quot;/search&quot; name=&quot;f&quot;&gt;&lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;&lt;tr valign=&quot;top&quot;&gt;&lt;td width=&quot;25%&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td align=&quot;center&quot; nowrap=&quot;nowrap&quot;&gt;&lt;input name=&quot;ie&quot; value=&quot;ISO-8859-1&quot; type=&quot;hidden&quot;&gt;&lt;input value=&quot;en&quot; name=&quot;hl&quot; type=&quot;hidden&quot;&gt;&lt;input name=&quot;source&quot; type=&quot;hidden&quot; value=&quot;hp&quot;&gt;&lt;div class=&quot;ds&quot; style=&quot;height:32px;margin:4px 0&quot;&gt;&lt;input autocomplete=&quot;off&quot; class=&quot;lst&quot; value=&quot;&quot; title=&quot;Google Search&quot; maxlength=&quot;2048&quot; name=&quot;q&quot; size=&quot;57&quot; style=&quot;color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top&quot;&gt;&lt;/div&gt;&lt;br style=&quot;line-height:0&quot;&gt;&lt;span class=&quot;ds&quot;&gt;&lt;span class=&quot;lsbb&quot;&gt;&lt;input class=&quot;lsb&quot; value=&quot;Google Search&quot; name=&quot;btnG&quot; type=&quot;submit&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ds&quot;&gt;&lt;span class=&quot;lsbb&quot;&gt;&lt;input class=&quot;lsb&quot; value=&quot;I'm Feeling Lucky&quot; name=&quot;btnI&quot; type=&quot;submit&quot; onclick=&quot;if(this.form.q.value)this.checked=1; else top.location='/doodles/'&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;&lt;td class=&quot;fl sblc&quot; align=&quot;left&quot; nowrap=&quot;nowrap&quot; width=&quot;25%&quot;&gt;&lt;a href=&quot;/advanced_search?hl=en&amp;amp;authuser=0&quot;&gt;Advanced search&lt;/a&gt;&lt;a href=&quot;/language_tools?hl=en&amp;amp;authuser=0&quot;&gt;Language tools&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;input type=&quot;hidden&quot; id=&quot;gbv&quot; name=&quot;gbv&quot; value=&quot;1&quot;&gt;&lt;/form&gt;&lt;div id=&quot;gac_scont&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-size:83%;min-height:3.5em&quot;&gt;&lt;br&gt;&lt;/div&gt;&lt;span id=&quot;footer&quot;&gt;&lt;div style=&quot;font-size:10pt&quot;&gt;&lt;div id=&quot;fll&quot; style=&quot;margin:19px auto;text-align:center&quot;&gt;&lt;a href=&quot;/intl/en/ads/&quot;&gt;Advertising&amp;nbsp;Programs&lt;/a&gt;&lt;a href=&quot;/services/&quot;&gt;Business Solutions&lt;/a&gt;&lt;a href=&quot;https://plus.google.com/116899029375914044550&quot; rel=&quot;publisher&quot;&gt;+Google&lt;/a&gt;&lt;a href=&quot;/intl/en/about.html&quot;&gt;About Google&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;p style=&quot;color:#767676;font-size:8pt&quot;&gt;&amp;copy; 2012 - &lt;a href=&quot;/intl/en/policies/&quot;&gt;Privacy &amp; Terms&lt;/a&gt;&lt;/p&gt;&lt;/span&gt;&lt;/center&gt;&lt;div id=xjsd&gt;&lt;/div&gt;&lt;div id=xjsi&gt;&lt;script&gt;if(google.y)google.y.first=[];(function(){var b;function c(a){window.setTimeout(function(){var d=document.createElement(&quot;script&quot;);d.src=a;document.getElementById(&quot;xjsd&quot;).appendChild(d)},0)}google.dljp=function(a){b=a;google.xjsi||(google.xjsu=a,c(b))};google.dlj=c;})();
if(!google.xjs){google.dstr=[];google.rein=[];window._=window._||{};window._._DumpException=function(e){throw e};if(google.timers&amp;&amp;google.timers.load.t){google.timers.load.t.xjsls=new Date().getTime();}google.dljp('/xjs/_/js/hp/sb_he,pcc/rt\x3dj/ver\x3ddUr8sRKOHrQ.en_US./d\x3d1/sv\x3d1/rs\x3dAItRSTMOuVd64TXf59sLZpeyfeaOnqHKgQ');google.xjs=1;}google.pmc={sb:{&quot;agen&quot;:false,&quot;cgen&quot;:true,&quot;client&quot;:&quot;heirloom-hp&quot;,&quot;dh&quot;:true,&quot;ds&quot;:&quot;&quot;,&quot;eqch&quot;:true,&quot;fl&quot;:true,&quot;host&quot;:&quot;google.com&quot;,&quot;jsonp&quot;:true,&quot;msgs&quot;:{&quot;lcky&quot;:&quot;I\u0026#39;m Feeling Lucky&quot;,&quot;lml&quot;:&quot;Learn more&quot;,&quot;oskt&quot;:&quot;Input tools&quot;,&quot;psrc&quot;:&quot;This search was removed from your \u003Ca href=\&quot;/history\&quot;\u003EWeb History\u003C/a\u003E&quot;,&quot;psrl&quot;:&quot;Remove&quot;,&quot;sbit&quot;:&quot;Search by image&quot;,&quot;srch&quot;:&quot;Google Search&quot;},&quot;ovr&quot;:{&quot;l&quot;:1,&quot;ms&quot;:1},&quot;pq&quot;:&quot;&quot;,&quot;qcpw&quot;:false,&quot;scd&quot;:10,&quot;sce&quot;:5,&quot;stok&quot;:&quot;2TLNAUa1t9yVii-aHCy9IeO1J4M&quot;},hp:{},pcc:{}};google.y.first.push(function(){if(google.med){google.med('init');google.initHistory();google.med('history');}google.History&amp;&amp;google.History.initialize('/');google.hs&amp;&amp;google.hs.init&amp;&amp;google.hs.init()});if(google.j&amp;&amp;google.j.en&amp;&amp;google.j.xi){window.setTimeout(google.j.xi,0);}&lt;/script&gt;&lt;/div&gt;&lt;script&gt;(function(){var b,c,d,e;function g(a,f){a.removeEventListener?(a.removeEventListener(&quot;load&quot;,f,!1),a.removeEventListener(&quot;error&quot;,f,!1)):(a.detachEvent(&quot;onload&quot;,f),a.detachEvent(&quot;onerror&quot;,f))}function h(a){e=(new Date).getTime();++c;a=a||window.event;a=a.target||a.srcElement;g(a,h)}var k=document.getElementsByTagName(&quot;img&quot;);b=k.length;
for(var l=c=0,m;l&lt;b;++l)m=k[l],m.complete||&quot;string&quot;!=typeof m.src||!m.src?++c:m.addEventListener?(m.addEventListener(&quot;load&quot;,h,!1),m.addEventListener(&quot;error&quot;,h,!1)):(m.attachEvent(&quot;onload&quot;,h),m.attachEvent(&quot;onerror&quot;,h));d=b-c;
function n(){if(google.timers.load.t){google.timers.load.t.ol=(new Date).getTime();google.timers.load.t.iml=e;google.kCSI.imc=c;google.kCSI.imn=b;google.kCSI.imp=d;void 0!==google.stt&amp;&amp;(google.kCSI.stt=google.stt);google.csiReport&amp;&amp;google.csiReport()}}window.addEventListener?window.addEventListener(&quot;load&quot;,n,!1):window.attachEvent&amp;&amp;window.attachEvent(&quot;onload&quot;,n);google.timers.load.t.prt=e=(new Date).getTime();})();
&lt;/script&gt;&lt;/body&gt;&lt;/html&gt;
&gt;&gt;&gt; import requests
&gt;&gt;&gt; r = requests.get('https://google.com')
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/api.py&quot;, line 55, in get
    return request('get', url, **kwargs)
  File &quot;/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/api.py&quot;, line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File &quot;/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/sessions.py&quot;, line 279, in request
    resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
  File &quot;/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/sessions.py&quot;, line 374, in send
    r = adapter.send(request, **kwargs)
  File &quot;/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/adapters.py&quot;, line 209, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='proxy.com', port=8080): Max retries exceeded with url: https://google.com/ (Caused by &lt;class 'socket.error'&gt;: [Errno 54] Connection reset by peer)
&gt;&gt;&gt; p = {'https': 'https://proxy.com:8080',}
&gt;&gt;&gt; r = requests.get('https://google.com', proxies=p)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/api.py&quot;, line 55, in get
    return request('get', url, **kwargs)
  File &quot;/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/api.py&quot;, line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File &quot;/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/sessions.py&quot;, line 279, in request
    resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
  File &quot;/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/sessions.py&quot;, line 374, in send
    r = adapter.send(request, **kwargs)
  File &quot;/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/adapters.py&quot;, line 209, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='proxy.com', port=8080): Max retries exceeded with url: https://google.com/ (Caused by &lt;class 'socket.error'&gt;: [Errno 54] Connection reset by peer)"><pre class="notranslate"><code class="notranslate">(osx)gfairchild@stueyemac ~&gt; set | grep -i proxy
HTTPS_PROXY=https://proxy.com:8080
https_proxy=https://proxy.com:8080
(osx)gfairchild@stueyemac ~&gt; python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import urllib2
&gt;&gt;&gt; r = urllib2.urlopen('https://google.com')
&gt;&gt;&gt; print r.read()
&lt;!doctype html&gt;&lt;html itemscope="itemscope" itemtype="http://schema.org/WebPage"&gt;&lt;head&gt;&lt;meta content="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for." name="description"&gt;&lt;meta content="noodp" name="robots"&gt;&lt;meta itemprop="image" content="/images/google_favicon_128.png"&gt;&lt;title&gt;Google&lt;/title&gt;&lt;script&gt;(function(){
window.google={kEI:"I-gsUdvaLMn-0gHWhIHIAg",getEI:function(a){for(var b;a&amp;&amp;(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||google.kEI},https:function(){return"https:"==window.location.protocol},kEXPI:"17259,18168,39523,4000116,4001569,4001948,4001959,4001975,4002001,4002159,4002562,4002734,4002855,4002858,4003372,4003374,4003387,4003514,4003575,4003638,4003917,4003944,4003982,4004015,4004064,4004074,4004083,4004152,4004181,4004214,4004241,4004276,4004298",kCSI:{e:"17259,18168,39523,4000116,4001569,4001948,4001959,4001975,4002001,4002159,4002562,4002734,4002855,4002858,4003372,4003374,4003387,4003514,4003575,4003638,4003917,4003944,4003982,4004015,4004064,4004074,4004083,4004152,4004181,4004214,4004241,4004276,4004298",ei:"I-gsUdvaLMn-0gHWhIHIAg"},authuser:0,ml:function(){},kHL:"en",time:function(){return(new Date).getTime()},log:function(a,
b,c,k){var d=new Image,f=google.lc,e=google.li,g="";d.onerror=d.onload=d.onabort=function(){delete f[e]};f[e]=d;!c&amp;&amp;-1==b.search("&amp;ei=")&amp;&amp;(g="&amp;ei="+google.getEI(k));c=c||"/gen_204?atyp=i&amp;ct="+a+"&amp;cad="+b+g+"&amp;zx="+google.time();a=/^http:/i;a.test(c)&amp;&amp;google.https()?(google.ml(Error("GLMM"),!1,{src:c}),delete f[e]):(d.src=c,google.li=e+1)},lc:[],li:0,Toolbelt:{},y:{},x:function(a,b){google.y[a.id]=[a,b];return!1},load:function(a,b){google.x({id:"l"+a},function(){google.load(a,b)})}};
})();
(function(){var d=!1;google.sn="webhp";google.timers={};google.startTick=function(a,b){google.timers[a]={t:{start:google.time()},bfr:!!b}};google.tick=function(a,b,h){google.timers[a]||google.startTick(a);google.timers[a].t[b]=h||google.time()};google.startTick("load",!0);
try{}catch(e){}})();
var _gjwl=location;function _gjuc(){var a=_gjwl.href.indexOf("#");if(0&lt;=a&amp;&amp;(a=_gjwl.href.substring(a),0&lt;a.indexOf("&amp;q=")||0&lt;=a.indexOf("#q=")))if(a=a.substring(1),-1==a.indexOf("#")){for(var d=0;d&lt;a.length;){var b=d;"&amp;"==a.charAt(b)&amp;&amp;++b;var c=a.indexOf("&amp;",b);-1==c&amp;&amp;(c=a.length);b=a.substring(b,c);if(0==b.indexOf("fp="))a=a.substring(0,d)+a.substring(c,a.length),c=d;else if("cad=h"==b)return 0;d=c}_gjwl.href="/search?"+a+"&amp;cad=h";return 1}return 0}
function _gjp(){(!window._gjwl.hash||!window._gjuc())&amp;&amp;setTimeout(_gjp,500)};
window._gjp&amp;&amp;_gjp();&lt;/script&gt;&lt;style&gt;#gbar,#guser{font-size:13px;padding-top:1px !important;}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22;margin-right:.5em;vertical-align:top}#gbar{float:left}}a.gb1,a.gb4{text-decoration:underline !important}a.gb1,a.gb4{color:#00c !important}.gbi .gb4{color:#dd8e27 !important}.gbf .gb4{color:#900 !important}&lt;/style&gt;&lt;style&gt;.h{font-family:arial,sans-serif}body{font-family:arial,sans-serif}td{font-family:arial,sans-serif}a{font-family:arial,sans-serif}p{font-family:arial,sans-serif}body{margin:0;overflow-y:scroll}#gog{padding:3px 8px 0}.h{color:#36c}.q{color:#00c}.ts{border-collapse:collapse}td{line-height:.8em}.gac_m td{line-height:17px}form{margin-bottom:20px}.ts td{padding:0}em{font-weight:bold;font-style:normal}.lst{height:25px;width:496px;font:18px arial,sans-serif}.gsfi{font:18px arial,sans-serif}.gsfs{font:17px arial,sans-serif}.ds{display:inline-box;display: inline-block;margin:3px 0 4px;margin-left:4px}input{font-family:inherit}body{background:#fff;color:black}a.gb1{color:#11c !important}a.gb2{color:#11c !important}a.gb3{color:#11c !important}a.gb4{color:#11c !important}.sblc{padding-top:5px}.lsbb{background:#eee;border:solid 1px;border-color:#ccc #999 #999 #ccc;height:30px}a{color:#11c;text-decoration:none}a:hover{text-decoration:underline}a:active{text-decoration:underline}.fl a{color:#36c}a:visited{color:#551a8b}a.gb1{text-decoration:underline}a.gb4{text-decoration:underline}a.gb3:hover{text-decoration:none}.sblc a{display:block;margin:2px 0;margin-left:13px;font-size:11px}#ghead a.gb2:hover{color:#fff !important}.lsbb{display:block}.ftl{display:inline-block;margin:0 12px}.lsb{background:url(/images/srpr/nav_logo80.png) 0 -258px repeat-x;border:none;color:#000;cursor:pointer;height:30px;margin:0;outline:0;font:15px arial,sans-serif;vertical-align:top}#fll a{display:inline-block;margin:0 12px}.lsb:active{background:#ccc}.lst:focus{outline:none}#addlang a{padding:0 3px}&lt;/style&gt;&lt;script&gt;&lt;/script&gt; &lt;/head&gt;&lt;body dir="ltr" bgcolor="#fff"&gt;&lt;script&gt;(function(){var src='/images/srpr/nav_logo80.png';var iesg=false;document.body.onload = function(){window.n &amp;&amp; window.n();if (document.images){new Image().src=src;}
if (!iesg){document.f&amp;&amp;document.f.q.focus();document.gbqf&amp;&amp;document.gbqf.q.focus();}
}
})();&lt;/script&gt;&lt;textarea id="csi" style="display:none"&gt;&lt;/textarea&gt;&lt;div id="mngb"&gt;&lt;div id=gbar&gt;&lt;nobr&gt;&lt;b class=gb1&gt;Search&lt;/b&gt; &lt;a class=gb1 href="https://www.google.com/imghp?hl=en&amp;tab=wi"&gt;Images&lt;/a&gt; &lt;a class=gb1 href="https://maps.google.com/maps?hl=en&amp;tab=wl"&gt;Maps&lt;/a&gt; &lt;a class=gb1 href="https://play.google.com/?hl=en&amp;tab=w8"&gt;Play&lt;/a&gt; &lt;a class=gb1 href="https://www.youtube.com/?tab=w1"&gt;YouTube&lt;/a&gt; &lt;a class=gb1 href="https://news.google.com/nwshp?hl=en&amp;tab=wn"&gt;News&lt;/a&gt; &lt;a class=gb1 href="https://mail.google.com/mail/?tab=wm"&gt;Gmail&lt;/a&gt; &lt;a class=gb1 href="https://drive.google.com/?tab=wo"&gt;Drive&lt;/a&gt; &lt;a class=gb1 style="text-decoration:none" href="http://www.google.com/intl/en/options/"&gt;&lt;u&gt;More&lt;/u&gt; &amp;raquo;&lt;/a&gt;&lt;/nobr&gt;&lt;/div&gt;&lt;div id=guser width=100%&gt;&lt;nobr&gt;&lt;span id=gbn class=gbi&gt;&lt;/span&gt;&lt;span id=gbf class=gbf&gt;&lt;/span&gt;&lt;span id=gbe&gt;&lt;/span&gt;&lt;a href="http://www.google.com/history/optout?hl=en" class=gb4&gt;Web History&lt;/a&gt; | &lt;a  href="/preferences?hl=en" class=gb4&gt;Settings&lt;/a&gt; | &lt;a target=_top id=gb_70 href="https://accounts.google.com/ServiceLogin?hl=en&amp;continue=https://www.google.com/" class=gb4&gt;Sign in&lt;/a&gt;&lt;/nobr&gt;&lt;/div&gt;&lt;div class=gbh style=left:0&gt;&lt;/div&gt;&lt;div class=gbh style=right:0&gt;&lt;/div&gt;&lt;/div&gt;&lt;iframe name="wgjf" style="display:none"&gt;&lt;/iframe&gt;&lt;center&gt;&lt;br clear="all" id="lgpd"&gt;&lt;div id="lga"&gt;&lt;img alt="Google" height="95" src="/intl/en_ALL/images/srpr/logo1w.png" width="275" id="hplogo" onload="window.lol&amp;&amp;lol()" style="padding:28px 0 14px"&gt;&lt;br&gt;&lt;br&gt;&lt;/div&gt;&lt;form action="/search" name="f"&gt;&lt;table cellpadding="0" cellspacing="0"&gt;&lt;tr valign="top"&gt;&lt;td width="25%"&gt;&amp;nbsp;&lt;/td&gt;&lt;td align="center" nowrap="nowrap"&gt;&lt;input name="ie" value="ISO-8859-1" type="hidden"&gt;&lt;input value="en" name="hl" type="hidden"&gt;&lt;input name="source" type="hidden" value="hp"&gt;&lt;div class="ds" style="height:32px;margin:4px 0"&gt;&lt;input autocomplete="off" class="lst" value="" title="Google Search" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top"&gt;&lt;/div&gt;&lt;br style="line-height:0"&gt;&lt;span class="ds"&gt;&lt;span class="lsbb"&gt;&lt;input class="lsb" value="Google Search" name="btnG" type="submit"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="ds"&gt;&lt;span class="lsbb"&gt;&lt;input class="lsb" value="I'm Feeling Lucky" name="btnI" type="submit" onclick="if(this.form.q.value)this.checked=1; else top.location='/doodles/'"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;&lt;td class="fl sblc" align="left" nowrap="nowrap" width="25%"&gt;&lt;a href="/advanced_search?hl=en&amp;amp;authuser=0"&gt;Advanced search&lt;/a&gt;&lt;a href="/language_tools?hl=en&amp;amp;authuser=0"&gt;Language tools&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;input type="hidden" id="gbv" name="gbv" value="1"&gt;&lt;/form&gt;&lt;div id="gac_scont"&gt;&lt;/div&gt;&lt;div style="font-size:83%;min-height:3.5em"&gt;&lt;br&gt;&lt;/div&gt;&lt;span id="footer"&gt;&lt;div style="font-size:10pt"&gt;&lt;div id="fll" style="margin:19px auto;text-align:center"&gt;&lt;a href="/intl/en/ads/"&gt;Advertising&amp;nbsp;Programs&lt;/a&gt;&lt;a href="/services/"&gt;Business Solutions&lt;/a&gt;&lt;a href="https://plus.google.com/116899029375914044550" rel="publisher"&gt;+Google&lt;/a&gt;&lt;a href="/intl/en/about.html"&gt;About Google&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;p style="color:#767676;font-size:8pt"&gt;&amp;copy; 2012 - &lt;a href="/intl/en/policies/"&gt;Privacy &amp; Terms&lt;/a&gt;&lt;/p&gt;&lt;/span&gt;&lt;/center&gt;&lt;div id=xjsd&gt;&lt;/div&gt;&lt;div id=xjsi&gt;&lt;script&gt;if(google.y)google.y.first=[];(function(){var b;function c(a){window.setTimeout(function(){var d=document.createElement("script");d.src=a;document.getElementById("xjsd").appendChild(d)},0)}google.dljp=function(a){b=a;google.xjsi||(google.xjsu=a,c(b))};google.dlj=c;})();
if(!google.xjs){google.dstr=[];google.rein=[];window._=window._||{};window._._DumpException=function(e){throw e};if(google.timers&amp;&amp;google.timers.load.t){google.timers.load.t.xjsls=new Date().getTime();}google.dljp('/xjs/_/js/hp/sb_he,pcc/rt\x3dj/ver\x3ddUr8sRKOHrQ.en_US./d\x3d1/sv\x3d1/rs\x3dAItRSTMOuVd64TXf59sLZpeyfeaOnqHKgQ');google.xjs=1;}google.pmc={sb:{"agen":false,"cgen":true,"client":"heirloom-hp","dh":true,"ds":"","eqch":true,"fl":true,"host":"google.com","jsonp":true,"msgs":{"lcky":"I\u0026#39;m Feeling Lucky","lml":"Learn more","oskt":"Input tools","psrc":"This search was removed from your \u003Ca href=\"/history\"\u003EWeb History\u003C/a\u003E","psrl":"Remove","sbit":"Search by image","srch":"Google Search"},"ovr":{"l":1,"ms":1},"pq":"","qcpw":false,"scd":10,"sce":5,"stok":"2TLNAUa1t9yVii-aHCy9IeO1J4M"},hp:{},pcc:{}};google.y.first.push(function(){if(google.med){google.med('init');google.initHistory();google.med('history');}google.History&amp;&amp;google.History.initialize('/');google.hs&amp;&amp;google.hs.init&amp;&amp;google.hs.init()});if(google.j&amp;&amp;google.j.en&amp;&amp;google.j.xi){window.setTimeout(google.j.xi,0);}&lt;/script&gt;&lt;/div&gt;&lt;script&gt;(function(){var b,c,d,e;function g(a,f){a.removeEventListener?(a.removeEventListener("load",f,!1),a.removeEventListener("error",f,!1)):(a.detachEvent("onload",f),a.detachEvent("onerror",f))}function h(a){e=(new Date).getTime();++c;a=a||window.event;a=a.target||a.srcElement;g(a,h)}var k=document.getElementsByTagName("img");b=k.length;
for(var l=c=0,m;l&lt;b;++l)m=k[l],m.complete||"string"!=typeof m.src||!m.src?++c:m.addEventListener?(m.addEventListener("load",h,!1),m.addEventListener("error",h,!1)):(m.attachEvent("onload",h),m.attachEvent("onerror",h));d=b-c;
function n(){if(google.timers.load.t){google.timers.load.t.ol=(new Date).getTime();google.timers.load.t.iml=e;google.kCSI.imc=c;google.kCSI.imn=b;google.kCSI.imp=d;void 0!==google.stt&amp;&amp;(google.kCSI.stt=google.stt);google.csiReport&amp;&amp;google.csiReport()}}window.addEventListener?window.addEventListener("load",n,!1):window.attachEvent&amp;&amp;window.attachEvent("onload",n);google.timers.load.t.prt=e=(new Date).getTime();})();
&lt;/script&gt;&lt;/body&gt;&lt;/html&gt;
&gt;&gt;&gt; import requests
&gt;&gt;&gt; r = requests.get('https://google.com')
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/api.py", line 55, in get
    return request('get', url, **kwargs)
  File "/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/sessions.py", line 279, in request
    resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
  File "/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/sessions.py", line 374, in send
    r = adapter.send(request, **kwargs)
  File "/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/adapters.py", line 209, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='proxy.com', port=8080): Max retries exceeded with url: https://google.com/ (Caused by &lt;class 'socket.error'&gt;: [Errno 54] Connection reset by peer)
&gt;&gt;&gt; p = {'https': 'https://proxy.com:8080',}
&gt;&gt;&gt; r = requests.get('https://google.com', proxies=p)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/api.py", line 55, in get
    return request('get', url, **kwargs)
  File "/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/sessions.py", line 279, in request
    resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
  File "/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/sessions.py", line 374, in send
    r = adapter.send(request, **kwargs)
  File "/Users/gfairchild/Documents/work/compepi/Twitter/lib/osx/lib/python2.7/site-packages/requests/adapters.py", line 209, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='proxy.com', port=8080): Max retries exceeded with url: https://google.com/ (Caused by &lt;class 'socket.error'&gt;: [Errno 54] Connection reset by peer)
</code></pre></div>
<p dir="auto">Sending a POST request with an empty body read from an empty file and a <code class="notranslate">Content-Length: 0</code> header times out after a long time instead of doing normal processing.</p>
<h2 dir="auto">Expected Result</h2>
<p dir="auto">Sending an empty body from a file with <code class="notranslate">Content-Length: 0</code> should send the request and return a result in a similar amount of time as a non-empty request, or a request without <code class="notranslate">Content-Length: 0</code>, or an empty body provided as a string.</p>
<p dir="auto">For example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ cat repro2.py 
import requests, tempfile

with tempfile.NamedTemporaryFile() as tf:
    print requests.Session().send(requests.Request('POST', 'https://www.google.com', data=open(tf.name)).prepare())

$ python repro2.py 
&lt;Response [405]&gt;"><pre class="notranslate"><code class="notranslate">$ cat repro2.py 
import requests, tempfile

with tempfile.NamedTemporaryFile() as tf:
    print requests.Session().send(requests.Request('POST', 'https://www.google.com', data=open(tf.name)).prepare())

$ python repro2.py 
&lt;Response [405]&gt;
</code></pre></div>
<p dir="auto">Or using a string body:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ cat repro3.py 
import requests, tempfile

print requests.Session().send(requests.Request('POST', 'https://www.google.com', headers={'Content-Length': u'0'}, data='').prepare())

$ python repro3.py 
&lt;Response [405]&gt;"><pre class="notranslate"><code class="notranslate">$ cat repro3.py 
import requests, tempfile

print requests.Session().send(requests.Request('POST', 'https://www.google.com', headers={'Content-Length': u'0'}, data='').prepare())

$ python repro3.py 
&lt;Response [405]&gt;
</code></pre></div>
<p dir="auto">Or a non-empty file:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ cat repro5.py 
import requests, tempfile

with tempfile.NamedTemporaryFile() as tf:
    tf.write('x')
    tf.flush()
    print requests.post('https://www.google.com', headers={'Content-Length': u'0'}, data=open(tf.name))

$ python repro5.py 
&lt;Response [405]&gt;"><pre class="notranslate"><code class="notranslate">$ cat repro5.py 
import requests, tempfile

with tempfile.NamedTemporaryFile() as tf:
    tf.write('x')
    tf.flush()
    print requests.post('https://www.google.com', headers={'Content-Length': u'0'}, data=open(tf.name))

$ python repro5.py 
&lt;Response [405]&gt;
</code></pre></div>
<h2 dir="auto">Actual Result</h2>
<p dir="auto">However, with the specific combination of a <code class="notranslate">Content-Length: 0</code> header and an empty open file descriptor, the request times out after 4 minutes with an error.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ pip freeze | grep requests
requests==2.18.2
$ cat repro1.py
import requests, tempfile

with tempfile.NamedTemporaryFile() as tf:
    requests.Session().send(requests.Request('POST', 'https://www.google.com', headers={'Content-Length': u'0'}, data=open(tf.name)).prepare())
$ time python repro1.py 
Traceback (most recent call last):
  File &quot;repro1.py&quot;, line 4, in &lt;module&gt;
    requests.Session().send(requests.Request('POST', 'https://www.google.com', headers={'Content-Length': u'0'}, data=open(tf.name)).prepare())
  File &quot;/home/ubuntu/env/lib/python2.7/site-packages/requests/sessions.py&quot;, line 612, in send
    r = adapter.send(request, **kwargs)
  File &quot;/home/ubuntu/env/lib/python2.7/site-packages/requests/adapters.py&quot;, line 490, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine(&quot;''&quot;,))

real	4m0.480s
user	0m0.296s
sys	0m0.040s
$ cat repro4.py 
import requests, tempfile

with tempfile.NamedTemporaryFile() as tf:
    print requests.post('https://www.google.com', headers={'Content-Length': u'0'}, data=open(tf.name))

$ time python repro4.py 
Traceback (most recent call last):
  File &quot;repro4.py&quot;, line 4, in &lt;module&gt;
    print requests.post('https://www.google.com', headers={'Content-Length': u'0'}, data=open(tf.name))
  File &quot;/home/ubuntu/env/lib/python2.7/site-packages/requests/api.py&quot;, line 112, in post
    return request('post', url, data=data, json=json, **kwargs)
  File &quot;/home/ubuntu/env/lib/python2.7/site-packages/requests/api.py&quot;, line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File &quot;/home/ubuntu/env/lib/python2.7/site-packages/requests/sessions.py&quot;, line 502, in request
    resp = self.send(prep, **send_kwargs)
  File &quot;/home/ubuntu/env/lib/python2.7/site-packages/requests/sessions.py&quot;, line 612, in send
    r = adapter.send(request, **kwargs)
  File &quot;/home/ubuntu/env/lib/python2.7/site-packages/requests/adapters.py&quot;, line 490, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine(&quot;''&quot;,))

real	4m5.474s
user	0m0.288s
sys	0m0.036s
"><pre class="notranslate"><code class="notranslate">$ pip freeze | grep requests
requests==2.18.2
$ cat repro1.py
import requests, tempfile

with tempfile.NamedTemporaryFile() as tf:
    requests.Session().send(requests.Request('POST', 'https://www.google.com', headers={'Content-Length': u'0'}, data=open(tf.name)).prepare())
$ time python repro1.py 
Traceback (most recent call last):
  File "repro1.py", line 4, in &lt;module&gt;
    requests.Session().send(requests.Request('POST', 'https://www.google.com', headers={'Content-Length': u'0'}, data=open(tf.name)).prepare())
  File "/home/ubuntu/env/lib/python2.7/site-packages/requests/sessions.py", line 612, in send
    r = adapter.send(request, **kwargs)
  File "/home/ubuntu/env/lib/python2.7/site-packages/requests/adapters.py", line 490, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",))

real	4m0.480s
user	0m0.296s
sys	0m0.040s
$ cat repro4.py 
import requests, tempfile

with tempfile.NamedTemporaryFile() as tf:
    print requests.post('https://www.google.com', headers={'Content-Length': u'0'}, data=open(tf.name))

$ time python repro4.py 
Traceback (most recent call last):
  File "repro4.py", line 4, in &lt;module&gt;
    print requests.post('https://www.google.com', headers={'Content-Length': u'0'}, data=open(tf.name))
  File "/home/ubuntu/env/lib/python2.7/site-packages/requests/api.py", line 112, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/home/ubuntu/env/lib/python2.7/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/ubuntu/env/lib/python2.7/site-packages/requests/sessions.py", line 502, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/ubuntu/env/lib/python2.7/site-packages/requests/sessions.py", line 612, in send
    r = adapter.send(request, **kwargs)
  File "/home/ubuntu/env/lib/python2.7/site-packages/requests/adapters.py", line 490, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",))

real	4m5.474s
user	0m0.288s
sys	0m0.036s

</code></pre></div>
<h2 dir="auto">Reproduction Steps</h2>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import requests, tempfile

with tempfile.NamedTemporaryFile() as tf:
    print requests.post('https://www.google.com', headers={'Content-Length': u'0'}, data=open(tf.name))"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">requests</span>, <span class="pl-s1">tempfile</span>

<span class="pl-k">with</span> <span class="pl-s1">tempfile</span>.<span class="pl-v">NamedTemporaryFile</span>() <span class="pl-k">as</span> <span class="pl-s1">tf</span>:
    <span class="pl-k">print</span> <span class="pl-s1">requests</span>.<span class="pl-en">post</span>(<span class="pl-s">'https://www.google.com'</span>, <span class="pl-s1">headers</span><span class="pl-c1">=</span>{<span class="pl-s">'Content-Length'</span>: <span class="pl-s">u'0'</span>}, <span class="pl-s1">data</span><span class="pl-c1">=</span><span class="pl-en">open</span>(<span class="pl-s1">tf</span>.<span class="pl-s1">name</span>))</pre></div>
<h2 dir="auto">System Information</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ python -m requests.help"><pre class="notranslate"><code class="notranslate">$ python -m requests.help
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
  &quot;chardet&quot;: {
    &quot;version&quot;: &quot;3.0.4&quot;
  }, 
  &quot;cryptography&quot;: {
    &quot;version&quot;: &quot;1.5.2&quot;
  }, 
  &quot;implementation&quot;: {
    &quot;name&quot;: &quot;CPython&quot;, 
    &quot;version&quot;: &quot;2.7.13&quot;
  }, 
  &quot;platform&quot;: {
    &quot;release&quot;: &quot;4.4.0-78-generic&quot;, 
    &quot;system&quot;: &quot;Linux&quot;
  }, 
  &quot;pyOpenSSL&quot;: {
    &quot;openssl_version&quot;: &quot;1000207f&quot;, 
    &quot;version&quot;: &quot;16.1.0&quot;
  }, 
  &quot;requests&quot;: {
    &quot;version&quot;: &quot;2.18.2&quot;
  }, 
  &quot;system_ssl&quot;: {
    &quot;version&quot;: &quot;1000207f&quot;
  }, 
  &quot;urllib3&quot;: {
    &quot;version&quot;: &quot;1.22&quot;
  }, 
  &quot;using_pyopenssl&quot;: true
}"><pre class="notranslate"><code class="notranslate">{
  "chardet": {
    "version": "3.0.4"
  }, 
  "cryptography": {
    "version": "1.5.2"
  }, 
  "implementation": {
    "name": "CPython", 
    "version": "2.7.13"
  }, 
  "platform": {
    "release": "4.4.0-78-generic", 
    "system": "Linux"
  }, 
  "pyOpenSSL": {
    "openssl_version": "1000207f", 
    "version": "16.1.0"
  }, 
  "requests": {
    "version": "2.18.2"
  }, 
  "system_ssl": {
    "version": "1000207f"
  }, 
  "urllib3": {
    "version": "1.22"
  }, 
  "using_pyopenssl": true
}
</code></pre></div>
<h2 dir="auto">Workaround</h2>
<p dir="auto">Check if the file is empty before sending and, if it is, substitute the file descriptor for an empty string.</p>