HTTPError... read the response body?

Stuart Davenport stuart.davenport at
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": "\"\""}

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

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

I thought that 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.


More information about the Python-list mailing list