Lost between urllib and httplib
dougfort at downright.com
Mon Jun 11 02:52:20 CEST 2001
Gustaf Liljegren wrote:
> 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
> h = httplib.HTTP(urlparse.urlparse(url))
> h.putrequest('GET', urlparse.urlparse(url))
> h.putheader('Accept', 'text/html')
> print "Host not found."
> print h.getreply()
> 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?
> Gustaf Liljegren
HTTP results 301 and 302 are redirections. You have to handle them
yourself using httplib. The good news is that httplib gives you the
flexibility to handle them as you want. I have attached one of our HTTP
clients that uses httplib. It handles all three sites you mentioned.
This code is used in our website load testing system http://www.stressmy.com
Doug Fort <dougfort at downright.com>
Senior Meat Manager
Downright Software LLC
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 14233 bytes
Desc: not available
More information about the Python-list