title: Python urllib2 not obtaining full response (PDF)
body: <p>I am trying to download a PDF by hitting a URL. Say my URL looks like this: <a href="http://foo.bar/this/downloads/pdf" rel="nofollow noreferrer">http://foo.bar/this/downloads/pdf</a></p>
  
  <p>If I hit the URL directly, the browser downloads the PDF, with no problem. However, if I try to get the PDF using <code>urllib2.urlopen</code> I get an incomplete file.</p>
  
  <pre><code>url = "http://foo.bar/this/downloads/pdf"
  sock = urllib2.urlopen(url)
  content = sock.read()
  with open('/tmp/test.pdf', 'w') as f:
      f.write(content)
  </code></pre>
  
  <p>The last 3 lines of /tmp/test.pdf look like this (and it looks like this in the variable <code>content</code>): </p>
  
  <pre><code>0000778731 00000 n 
  0000778751 00000 n 
  000
  </code></pre>
  
  <p>But the actual file that I downloaded from the browser looks like this:</p>
  
  <pre><code>0000778731 00000 n 
  0000778751 00000 n 
  0000778772 00000 n 
  ...
  %%EOF
  </code></pre>
  
  <p>Every single PDF, regardless of size, seems to cut off somewhere in this final combination of numbers.</p>
  
  <p>I have tried the following solutions, and both do not work. I believe the reason does not have to do with the way in which the data is read, but the fact that the <code>urllib2</code> is not even getting the full response in the first place.</p>
  
  <p><a href="https://stackoverflow.com/questions/10102696/python-not-getting-full-response">python,not getting full response</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1824069/urllib2-not-retrieving-entire-http-response">urllib2 not retrieving entire HTTP response</a></p>
  
  <p>Another thing that may be a factor (though I'm unsure) is the way the PDF is sent to the browser. To my knowledge, the PDF is sent using PHP x-sendfile. I am just confused as to why the PDF is partially downloaded.</p>