[ACCEPTED]-In Python, what does getresponse() return?-unix

Accepted answer
Score: 26

You can always inspect an object using dir; that 17 will show you which attributes it has.

>>> import httplib
>>> conn = httplib.HTTPConnection("www.google.nl")
>>> conn.request("HEAD", "/index.html")
>>> res = conn.getresponse()
>>> dir(res)
['__doc__', '__init__', '__module__', '_check_close', '_method', '_read_chunked', '_read_status', '_safe_read', 'begin', 'chunk_left', 'chunked', 'close', 'debuglevel', 'fp', 'getheader', 'getheaders', 'isclosed', 'length', 'msg', 'read', 'reason', 'status', 'strict', 'version', 'will_close']

Likewise, you 16 can invoke help, which will show an object's 15 documentation, if it has a __doc__ attribute. As 14 you can see, this is the case for res, so try:

>>> help(res)

Other 13 than that, the documentation states that 12 getresponse returns an HTTPResponse object. Thus, as you can read 11 there (and in help(res)), the following properties 10 and methods are defined on HTTPResponse objects:

  • HTTPResponse.read([amt]): Reads 9 and returns the response body, or up to 8 the next amt bytes.

  • HTTPResponse.getheader(name[, default]): Get the contents of 7 the header name, or default if there is 6 no matching header.

  • HTTPResponse.getheaders(): Return a list of (header, value) tuples. (New in version 2.4.)

  • HTTPResponse.msg: A 5 mimetools.Message instance containing the 4 response headers.

  • HTTPResponse.version: HTTP protocol version 3 used by server. 10 for HTTP/1.0, 11 for 2 HTTP/1.1.

  • HTTPResponse.status: Status code returned by server.

  • HTTPResponse.reason: Reason 1 phrase returned by server.

More Related questions