title: How can I fix biopython urllib.error.HTTPError 502?
body: <p>Hi I am using biopython to pull files from NCBI using Entrez. The program works on small files but on larger files I get an error. I would really appreciate some insight or help figuring out what went wrong.</p>
  
  <p>Here is the program: </p>
  
  <pre class="lang-py prettyprint-override"><code>from Bio import Entrez
  Entrez.email = "jbro262@lsu.edu"
  search_handle = Entrez.esearch(db="nucleotide",term="Saimiri",usehistory="n")
  search_results = Entrez.read(search_handle)
  search_handle.close()
  
  gi_list = search_results["IdList"]
  count = int(search_results["Count"])
  a = open("Numfile.txt", "a+")
  a.write("The number of Saimiri files are :")
  a.write(str(count))
  a.write("\n")
  a.close()
  
  webenv = search_results["WebEnv"]
  query_key = search_results["QueryKey"]
  
  batch_size = 25
  out_handle = open("SaimiriDNA.fasta", "w")
  
  for start in range(0,count,batch_size):
  
      end = min(count, start+batch_size)
      print("Going to download record %i to %i" % (start+1, end))
  
      fetch_handle = Entrez.efetch(db="nucleotide", rettype="fasta", retmode="text", retstart=start, retmax=batch_size, webenv=webenv, query_key=query_key)
      data=fetch_handle.read()
      fetch_handle.close()
      out_handle.write(data)
  out_handle.close()
  </code></pre>
  
  <p>Here are the errors:</p>
  
  <pre class="lang-none prettyprint-override"><code>Traceback (most recent call last):
    File "Entrezfiles_Saimiri.py", line 53, in &lt;module&gt;
      fetch_handle = Entrez.efetch(db="nucleotide", rettype="fasta", retmode="text", retstart=start, retmax=batch_size, webenv=webenv, query_key=query_key)
    File "/usr/local/lib/python3.4/
  
  dist-packages/Bio/Entrez/__init__.py", line 149, in efetch
      return _open(cgi, variables, post)
    File "/usr/local/lib/python3.4/dist-packages/Bio/Entrez/__init__.py", line 464, in _open
      raise exception
    File "/usr/local/lib/python3.4/dist-packages/Bio/Entrez/__init__.py", line 462, in _open
      handle = _urlopen(cgi)
    File "/usr/lib/python3.4/urllib/request.py", line 153, in urlopen
      return opener.open(url, data, timeout)
    File "/usr/lib/python3.4/urllib/request.py", line 461, in open
      response = meth(req, response)
    File "/usr/lib/python3.4/urllib/request.py", line 571, in http_response
      'http', request, response, code, msg, hdrs)
    File "/usr/lib/python3.4/urllib/request.py", line 499, in error
      return self._call_chain(*args)
    File "/usr/lib/python3.4/urllib/request.py", line 433, in _call_chain
      result = func(*args)
    File "/usr/lib/python3.4/urllib/request.py", line 579, in http_error_default
      raise HTTPError(req.full_url, code, msg, hdrs, fp)
  urllib.error.HTTPError: HTTP Error 502: Bad Gateway
  </code></pre>
  
  <p>Does this mean something is going wrong with my server while the files are downloading?</p>