StormShadow
StormShadow

Reputation: 1627

How do I get inside Python http.client.HTTPResponse objects?

I've tried dir(), but the commands just return messages like this:

<bound method HTTPResponse.begin of <http.client.HTTPResponse object at 0x00E9DEF0>>

which I'm afraid I don't know how to interpret.

Disclaimer: I haven't used Python much at all, so this may be a really stupid question. Please be gentle.

Thanks!

Upvotes: 1

Views: 5525

Answers (3)

Sushmita Poudel
Sushmita Poudel

Reputation: 45

HTTPResponse.read() function can be used to get the body of the HTTPResponse. The function HTTPresponse.read() reads the http.client.HTTPResponse object and returns the response body. More about it can be found in Python docs https://docs.python.org/3/library/http.client.html#httpresponse-objects

response = urllib.request.urlopen(some_request)
body = response.read()

Note:urllib.request.urlopen(some_request) returns HTTP response object

Upvotes: 1

Shasa
Shasa

Reputation: 81

You can print the contents of A HTTPResponse object line by line like this

# Get the object from a url
url = 'https://www.example.com'
response_object = urllib.request.urlopen(url)

# Print the contents line by line 
for line in response_object:
    print(line)

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799230

Methods, just as functions, must be followed by parens (()), optionally containing arguments, in order to invoke them.

someobj.somemeth()

Upvotes: 3

Related Questions