[Tutor] http return status when using urllib

Lloyd Kvam pythontutor at venix.com
Fri Oct 3 11:49:34 EDT 2003


I used urllib to post some data to a site and could not find any easy way
to determine the HTTP status.  When I supplied a deliberately wrong URL
I simply got back the Apache error page with no real indication that
a problem occurred.  I, of course, could read the error message, but
there was no simple mechanism for the script to know it had failed.

urllib2 raises an exception and includes attributes:
	code and msg
in the exception which reflect the HTTP status line.  Obviously,
the exception is enough to alert the script that a problem occurred.

There was no discussion of error handling in "Python in a Nutshell",
"Python Standard Library", or the module docs.  I read through the
urllib source code and the module clearly goes to extra lengths to
prevent an exception.  (Yes, I can override this behavior by
writing my own class.)

Am I missing something in my approach?  Does anyone know the
rationale for hiding errors (and the status code)?

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-443-6155
fax:	801-459-9582




More information about the Tutor mailing list