HTTP response code
fredrik at pythonware.com
Sun Dec 5 09:49:20 CET 2004
Jonas Galvez wrote:
> Hi list, here's a question about urllib. Is it possible to simply
> retrieve the HTTP responde code for a given URL? I don't want to
> download the body of the HTTP message. I simply want to check the
> response code, like, if it is 200, 301 etc. Something like:
> if urllib.urlopen(the_url).response_code == 200:
> # ...
> Is there an easy way to do this?
urllib processes the response code, and follows redirects etc. If urllib gets a
response code that it cannot deal with, it raises an exception (which includes
the error code).
If you want to check the response code for any given resource, use httplib
(see Ian's reply). If you're building a link checker of some kind, you might
prefer an asynchronous HTTP client (which lets you issue multiple requests
at once, and process the responses as they arrive):
More information about the Python-list