MCPcopy
hub / github.com/urllib3/urllib3 / getresponse

Method getresponse

src/urllib3/connection.py:540–600  ·  view source on GitHub ↗

Get the response from the server. If the HTTPConnection is in the correct state, returns an instance of HTTPResponse or of whatever object is returned by the response_class variable. If a request has not been sent or if a previous response has not be handled, ResponseNotRe

(  # type: ignore[override]
        self,
    )

Source from the content-addressed store, hash-verified

538 self.request(method, url, body=body, headers=headers, chunked=True)
539
540 def getresponse( # type: ignore[override]
541 self,
542 ) -> HTTPResponse:
543 """
544 Get the response from the server.
545
546 If the HTTPConnection is in the correct state, returns an instance of HTTPResponse or of whatever object is returned by the response_class variable.
547
548 If a request has not been sent or if a previous response has not be handled, ResponseNotReady is raised. If the HTTP response indicates that the connection should be closed, then it will be closed before the response is returned. When the connection is closed, the underlying socket is closed.
549 """
550 # Raise the same error as http.client.HTTPConnection
551 if self._response_options is None:
552 raise ResponseNotReady()
553
554 # Reset this attribute for being used again.
555 resp_options = self._response_options
556 self._response_options = None
557
558 # Since the connection's timeout value may have been updated
559 # we need to set the timeout on the socket.
560 self.sock.settimeout(self.timeout)
561
562 # This is needed here to avoid circular import errors
563 from .response import HTTPResponse
564
565 # Save a reference to the shutdown function before ownership is passed
566 # to httplib_response
567 # TODO should we implement it everywhere?
568 _shutdown = getattr(self.sock, "shutdown", None)
569
570 # Get the response from http.client.HTTPConnection
571 httplib_response = super().getresponse()
572
573 try:
574 assert_header_parsing(httplib_response.msg)
575 except (HeaderParsingError, TypeError) as hpe:
576 log.warning(
577 "Failed to parse headers (url=%s): %s",
578 _url_from_connection(self, resp_options.request_url),
579 hpe,
580 exc_info=True,
581 )
582
583 headers = HTTPHeaderDict(httplib_response.msg.items())
584
585 response = HTTPResponse(
586 body=httplib_response,
587 headers=headers,
588 status=httplib_response.status,
589 version=httplib_response.version,
590 version_string=getattr(self, "_http_vsn_str", "HTTP/?"),
591 reason=httplib_response.reason,
592 preload_content=resp_options.preload_content,
593 decode_content=resp_options.decode_content,
594 original_response=httplib_response,
595 enforce_content_length=resp_options.enforce_content_length,
596 request_method=resp_options.request_method,
597 request_url=resp_options.request_url,

Calls 6

assert_header_parsingFunction · 0.85
_url_from_connectionFunction · 0.85
HTTPHeaderDictClass · 0.85
HTTPResponseClass · 0.85
itemsMethod · 0.80
settimeoutMethod · 0.45