Missing feature in urllib? (Getting HTTP reply code)

Robert W. Bill rbill at digisprings.com
Mon Jul 31 14:09:50 EDT 2000


On Mon, 31 Jul 2000, Yves-Eric Martin wrote:
>     Hello everyone,
>     I am now using urllib to get some data from a HTTP server, say:
> 
> import urllib
> f = urllib.urlopen('http://www.nowhere.com/stuff')
> readData = f.read()
 <snip>

I didn't see other responses, so I took a quick look at urllib.  It didn't
seem to have any accessor for response code and it also didn't seem very 
`subclassing friendly'.  I would think that looking at httplib is a
better idea.

However:
Headers are handled OK with with urllib, so one
suggestion is to pollute the headers with the response codes in urllib.
A potential-5-minute-saving-alternative might be:

1. copy urllib.py to myurllib.py
2. find line (#266 in my version)(in class URLopener, method open_http)
	errcode, errmsg, headers = h.getreply()
3. add next line =   
	headers["Responsecode"] = str(errcode) + " " + errmsg

Then:
>>>import myurllib
>>>urlconnection = myurllib.urlopen('http://www.python.org/')
>>>urlconnection.headers["Responsecode"]
'200 OK'

-robert




More information about the Python-list mailing list