title: How to download a file using python in a 'smarter' way?
body: <p>I need to download several files via http in Python.</p>
  
  <p>The most obvious way to do it is just using urllib2:</p>
  
  <pre><code>import urllib2
  u = urllib2.urlopen('http://server.com/file.html')
  localFile = open('file.html', 'w')
  localFile.write(u.read())
  localFile.close()
  </code></pre>
  
  <p>But I'll have to deal with the URLs that are nasty in some way, say like this: <code>http://server.com/!Run.aspx/someoddtext/somemore?id=121&amp;m=pdf</code>. When downloaded via the browser, the file has a human-readable name, ie. <code>accounts.pdf</code>. </p>
  
  <p>Is there any way to handle that in python, so I don't need to know the file names and hardcode them into my script?</p>