How do I get remote http error code?
Hi, I'm struggling to find a way to return the http error code (i.e. 404, 401, 301, 201) using an External Script and urllib. I can read the file using urllib.urlopen(url).read() and can get general info using urllib.urlopen(url).info() - however I can't find how to return the error code (i.e if the remote page doesn't exist). Anyone know how to resolve this? Cheers, Duane
Hi, --On Mittwoch, 20. November 2002 13:54 +0000 Duane Raymond <Duane.Raymond@Virgin.net> wrote:
Hi,
I'm struggling to find a way to return the http error code (i.e. 404, 401, 301, 201) using an External Script and urllib.
I can read the file using urllib.urlopen(url).read() and can get general info using urllib.urlopen(url).info() - however I can't find how to return the error code (i.e if the remote page doesn't exist).
Anyone know how to resolve this?
I'm using an external method like this: from ZPublisher import Client def web_client(url, username = None, password = None, **kw): '''access http servers''' class gen_res: __allow_access_to_unprotected_subobjects__=1 f=gen_res() if kw: f.headers,f.body=apply(Client.call,(url,username,password),kw) else: f.headers,f.body=Client.call(url,username,password) return(f) where you get an exception if status code cannot be handled and have access to the returnvalues head (with headers) and body (what comes back from server as content) Regards Tino
Never mind - I just found an answer to my own question - it is posted below if anyone else needs it at some point
I'm struggling to find a way to return the http error code (i.e. 404, 401, 301, 201) using an External Script and urllib.
I can read the file using urllib.urlopen(url).read() and can get general info using urllib.urlopen(url).info() - however I can't find how to return the error code (i.e if the remote page doesn't exist).
Anyone know how to resolve this?
I used httplib (adapting code from http://www.python.org/doc/lib/httplib-examples.html) conn = httplib.HTTPConnection(urldomain) conn.request("GET", urlpath) r1 = conn.getresponse() pgStatus = [r1.status, r1.reason] where urldomain = domain name (www.xyz.com) and urlpath is any bit after the domain. Cheers, Duane
Duane Raymond wrote:
I used httplib (adapting code from http://www.python.org/doc/lib/httplib-examples.html)
conn = httplib.HTTPConnection(urldomain) conn.request("GET", urlpath) r1 = conn.getresponse() pgStatus = [r1.status, r1.reason]
where urldomain = domain name (www.xyz.com) and urlpath is any bit after the domain.
Getting this to work with HTTP basic auth is a bit of a PITA, though ;-) Chris
participants (3)
-
Chris Withers -
Duane Raymond -
Tino Wildenhain