HTTPError... read the response body?

Stuart Davenport stuart.davenport at gmail.com
Mon Mar 2 23:29:12 CET 2009


Hi There,

I am trying to connect to a web service but I am getting HTTP 400, I
am not too concerned about the HTTP error - but what I'd like to know
if there is anyway I can read the response body in the HTTP 400 or 500
case? Does the HTTPError allow this? or the urllib2 in anyway?

This is what I have at the moment...

import sys, urllib2
import socket

class APIConnector:
        def connect(*args):
                length = len(args[2])
                headers = {"Content-Length": length, "Content-Type":
"text/xml", "SOAPAction": "\"http://some.api.com/RemovedAction\""}

                try:
                        #url, body, headers
                        rq = urllib2.Request(args[1], args[3],
headers)
                        rs = urllib2.urlopen(rq)

                        print rs.read()
                except urllib2.HTTPError, e:
                        conn = e
                        print 'error', conn.code, 'message: ',
conn.msg, 'filename: ', conn.filename, 'headers: ', conn.hdrs, 'body:
', conn.read()

I thought that conn.read() may show the body, but it seems not... Can
anyone help please, or suggest another way around? I am using python
2.5 installed with MAC OS X Leopard.


Cheers
Stu



More information about the Python-list mailing list