Read and return the response body, or up to the next amt bytes.
(self, amt=None)
| 469 | return self.fp is None |
| 470 | |
| 471 | def read(self, amt=None): |
| 472 | """Read and return the response body, or up to the next amt bytes.""" |
| 473 | if self.fp is None: |
| 474 | return b"" |
| 475 | |
| 476 | if self._method == "HEAD": |
| 477 | self._close_conn() |
| 478 | return b"" |
| 479 | |
| 480 | if self.chunked: |
| 481 | return self._read_chunked(amt) |
| 482 | |
| 483 | if amt is not None and amt >= 0: |
| 484 | if self.length is not None and amt > self.length: |
| 485 | # clip the read to the "end of response" |
| 486 | amt = self.length |
| 487 | s = self.fp.read(amt) |
| 488 | if not s and amt: |
| 489 | # Ideally, we would raise IncompleteRead if the content-length |
| 490 | # wasn't satisfied, but it might break compatibility. |
| 491 | self._close_conn() |
| 492 | elif self.length is not None: |
| 493 | self.length -= len(s) |
| 494 | if not self.length: |
| 495 | self._close_conn() |
| 496 | return s |
| 497 | else: |
| 498 | # Amount is not given (unbounded read) so we must check self.length |
| 499 | if self.length is None: |
| 500 | s = self.fp.read() |
| 501 | else: |
| 502 | try: |
| 503 | s = self._safe_read(self.length) |
| 504 | except IncompleteRead: |
| 505 | self._close_conn() |
| 506 | raise |
| 507 | self.length = 0 |
| 508 | self._close_conn() # we read everything |
| 509 | return s |
| 510 | |
| 511 | def readinto(self, b): |
| 512 | """Read up to len(b) bytes into bytearray b and return the number |