Get the response from the server. If the HTTPConnection is in the correct state, returns an instance of HTTPResponse. If a request has not been sent or if a previous response has not be handled, ResponseNotReady is raised. If the HTTP response indicates tha
(self)
| 1408 | self.endheaders(body, encode_chunked=encode_chunked) |
| 1409 | |
| 1410 | def getresponse(self): |
| 1411 | """Get the response from the server. |
| 1412 | |
| 1413 | If the HTTPConnection is in the correct state, returns an |
| 1414 | instance of HTTPResponse. |
| 1415 | |
| 1416 | If a request has not been sent or if a previous response has |
| 1417 | not be handled, ResponseNotReady is raised. If the HTTP |
| 1418 | response indicates that the connection should be closed, then |
| 1419 | it will be closed before the response is returned. When the |
| 1420 | connection is closed, the underlying socket is closed. |
| 1421 | """ |
| 1422 | |
| 1423 | # if a prior response has been completed, then forget about it. |
| 1424 | if self.__response and self.__response.isclosed(): |
| 1425 | self.__response = None |
| 1426 | |
| 1427 | # if a prior response exists, then it must be completed (otherwise, we |
| 1428 | # cannot read this response's header to determine the connection-close |
| 1429 | # behavior) |
| 1430 | # |
| 1431 | # note: if a prior response existed, but was connection-close, then the |
| 1432 | # socket and response were made independent of this HTTPConnection |
| 1433 | # object since a new request requires that we open a whole new |
| 1434 | # connection |
| 1435 | # |
| 1436 | # this means the prior response had one of two states: |
| 1437 | # 1) will_close: this connection was reset and the prior socket and |
| 1438 | # response operate independently |
| 1439 | # 2) persistent: the response was retained and we await its |
| 1440 | # isclosed() status to become true. |
| 1441 | # |
| 1442 | if self.__state != _CS_REQ_SENT or self.__response: |
| 1443 | raise ResponseNotReady(self.__state) |
| 1444 | |
| 1445 | if self.debuglevel > 0: |
| 1446 | response = self.response_class(self.sock, self.debuglevel, |
| 1447 | method=self._method) |
| 1448 | else: |
| 1449 | response = self.response_class(self.sock, method=self._method) |
| 1450 | |
| 1451 | try: |
| 1452 | try: |
| 1453 | if self.max_response_headers is None: |
| 1454 | response.begin() |
| 1455 | else: |
| 1456 | response.begin(_max_headers=self.max_response_headers) |
| 1457 | except ConnectionError: |
| 1458 | self.close() |
| 1459 | raise |
| 1460 | assert response.will_close != _UNKNOWN |
| 1461 | self.__state = _CS_IDLE |
| 1462 | |
| 1463 | if response.will_close: |
| 1464 | # this effectively passes the connection to the response |
| 1465 | self.close() |
| 1466 | else: |
| 1467 | # remember this, so we can tell when it is complete |