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开发者_C百科 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!
Methods, just as functions, must be followed by parens (()
), optionally containing arguments, in order to invoke them.
someobj.somemeth()
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)
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
精彩评论