Scrapy, a fast high-level web crawling & scraping framework for Python.
Reponse.protocol as string
Eugenio Lacuesta5e9a99e6a1e9
diff --git a/docs/topics/request-response.rst b/docs/topics/request-response.rstindex 1cb724227..98906992d 100644--- a/docs/topics/request-response.rst+++ b/docs/topics/request-response.rst@@ -693,12 +693,9 @@ Response objects:param ip_address: The IP address of the server from which the Response originated.:type ip_address: :class:`ipaddress.IPv4Address` or :class:`ipaddress.IPv6Address`- :param protocol: A tuple containing information about the protocol that was used- to download the response. Taken from the ``version`` attribute of the- corresponding :class:`twisted.web.client.Response` object, it will tipically- consist of the protocol and version numbers, e.g. ``(b"HTTP", 1, 1)``- to represent "HTTP/1.1".- :type protocol: :class:`tuple`+ :param protocol: The protocol that was used to download the response.+ For instance: "HTTP/1.0", "HTTP/1.1"+ :type protocol: :class:`str`.. versionadded:: 2.0.0The ``certificate`` parameter.@@ -814,14 +811,11 @@ Response objects.. versionadded:: VERSION- A tuple containing information about the protocol that was used- to download the response. Taken from the ``version`` attribute of the- corresponding :class:`twisted.web.client.Response` object, it will tipically- consist of the protocol and version numbers, e.g. ``(b"HTTP", 1, 1)``- to represent "HTTP/1.1".+ The protocol that was used to download the response.+ For instance: "HTTP/1.0", "HTTP/1.1"- This attribute is currently only populated by the HTTP 1.1 download- handler, i.e. for ``http(s)`` responses. For other handlers,+ This attribute is currently only populated by the HTTP download+ handlers, i.e. for ``http(s)`` responses. For other handlers,:attr:`protocol` is always ``None``... method:: Response.copy()diff --git a/scrapy/core/downloader/handlers/http11.py b/scrapy/core/downloader/handlers/http11.pyindex 0f30b01f9..c7553eb87 100644--- a/scrapy/core/downloader/handlers/http11.py+++ b/scrapy/core/downloader/handlers/http11.py@@ -434,6 +434,11 @@ class ScrapyAgent:def _cb_bodydone(self, result, request, url):headers = Headers(result["txresponse"].headers.getAllRawHeaders())respcls = responsetypes.from_args(headers=headers, url=url, body=result["body"])+ try:+ version = result["txresponse"].version+ protocol = f"{to_unicode(version[0])}/{version[1]}.{version[2]}"+ except (AttributeError, TypeError):+ protocol = Noneresponse = respcls(url=url,status=int(result["txresponse"].code),@@ -442,7 +447,7 @@ class ScrapyAgent:flags=result["flags"],certificate=result["certificate"],ip_address=result["ip_address"],- protocol=getattr(result["txresponse"], "version", None),+ protocol=protocol,)if result.get("failure"):result["failure"].value.response = responsediff --git a/scrapy/core/downloader/webclient.py b/scrapy/core/downloader/webclient.pyindex c13683393..9524cce2b 100644--- a/scrapy/core/downloader/webclient.py+++ b/scrapy/core/downloader/webclient.py@@ -7,7 +7,7 @@ from twisted.internet.protocol import ClientFactoryfrom scrapy.http import Headersfrom scrapy.utils.httpobj import urlparse_cached-from scrapy.utils.python import to_bytes+from scrapy.utils.python import to_bytes, to_unicodefrom scrapy.responsetypes import responsetypes@@ -110,7 +110,7 @@ class ScrapyHTTPClientFactory(ClientFactory):status = int(self.status)headers = Headers(self.response_headers)respcls = responsetypes.from_args(headers=headers, url=self._url)- return respcls(url=self._url, status=status, headers=headers, body=body)+ return respcls(url=self._url, status=status, headers=headers, body=body, protocol=to_unicode(self.version))def _set_connection_attributes(self, request):parsed = urlparse_cached(request)diff --git a/tests/test_downloader_handlers.py b/tests/test_downloader_handlers.pyindex eb6d40df7..a8763a7a5 100644--- a/tests/test_downloader_handlers.py+++ b/tests/test_downloader_handlers.py@@ -360,6 +360,13 @@ class Http10TestCase(HttpTestCase):"""HTTP 1.0 test case"""download_handler_cls = HTTP10DownloadHandler+ def test_protocol(self):+ request = Request(self.getURL("host"), method="GET")+ d = self.download_request(request, Spider("foo"))+ d.addCallback(lambda r: r.protocol)+ d.addCallback(self.assertEqual, "HTTP/1.0")+ return d+class Https10TestCase(Http10TestCase):scheme = 'https'@@ -493,7 +500,7 @@ class Http11TestCase(HttpTestCase):request = Request(self.getURL("host"), method="GET")d = self.download_request(request, Spider("foo"))d.addCallback(lambda r: r.protocol)- d.addCallback(self.assertEqual, (b"HTTP", 1, 1))+ d.addCallback(self.assertEqual, "HTTP/1.1")return d