Lost between urllib and httplib

Gustaf Liljegren gustafl at algonet.se
Sun Jun 10 17:06:11 EDT 2001


Python 2.1:

I'm choosing between urllib and httplib, but have trouble with both. 
urllib.urlopen() is hiding HTTP errors (hope this will be fixed in next 
version!), and httplib.HTTP() doesn't seem to be able to access some pages 
if you only supply a domain name.

Here's the script I use for testing httplib:

import urlparse
import httplib
import sys

url = sys.argv[1]

try:
  h = httplib.HTTP(urlparse.urlparse(url)[1])
  h.putrequest('GET', urlparse.urlparse(url)[2])
  h.putheader('Accept', 'text/html')
  h.endheaders()
except:
  print "Host not found."
  sys.exit()

print h.getreply()[0]

On some websites, I get HTTP error 301, 302 or even 404. Try for example:

http://www.webstandards.org (301)
http://www.native-instruments.com (302)
http://www.chaos.com (404)

If using urllib however, these places are all accessed successfully. What 
is it in urllib.urlopen() that has to be added if using httplib?

Regards,

Gustaf Liljegren



More information about the Python-list mailing list