scrapy-proxy-headers 0.1.2__tar.gz → 0.1.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,11 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.1
2
2
  Name: scrapy-proxy-headers
3
- Version: 0.1.2
3
+ Version: 0.1.3
4
4
  Summary: Add custom proxy headers to HTTPS requests in Scrapy
5
5
  Author-email: ProxyMesh <support@proxymesh.com>
6
6
  Project-URL: Homepage, https://github.com/proxymesh/scrapy-proxy-headers
7
7
  Project-URL: Changelog, https://github.com/proxymesh/scrapy-proxy-headers/commits/main/
8
+ Project-URL: Issues, https://github.com/proxymesh/scrapy-proxy-headers/issues
8
9
  Classifier: Programming Language :: Python :: 3
9
10
  Classifier: Operating System :: OS Independent
10
11
  Classifier: License :: OSI Approved :: BSD License
@@ -23,7 +24,7 @@ Because Scrapy does not have a good way to pass custom headers to a proxy when y
23
24
 
24
25
  To use this extension, do the following:
25
26
 
26
- 1. `pip install scrapy_proxy_headers`
27
+ 1. `pip install scrapy-proxy-headers`
27
28
  2. In your Scrapy `settings.py`, add the following code:
28
29
 
29
30
  ```python
@@ -38,4 +39,4 @@ DOWNLOAD_HANDLERS = {
38
39
  request.meta["proxy_headers"] = {"X-ProxyMesh-Country": "US"}
39
40
  ```
40
41
 
41
- Currently this package does not yet support reading custom response headers from the proxy, but that feature is coming soon.
42
+ 4. Any response headers that might come from the proxy will be saved in `response.headers`
@@ -6,7 +6,7 @@ Because Scrapy does not have a good way to pass custom headers to a proxy when y
6
6
 
7
7
  To use this extension, do the following:
8
8
 
9
- 1. `pip install scrapy_proxy_headers`
9
+ 1. `pip install scrapy-proxy-headers`
10
10
  2. In your Scrapy `settings.py`, add the following code:
11
11
 
12
12
  ```python
@@ -21,4 +21,4 @@ DOWNLOAD_HANDLERS = {
21
21
  request.meta["proxy_headers"] = {"X-ProxyMesh-Country": "US"}
22
22
  ```
23
23
 
24
- Currently this package does not yet support reading custom response headers from the proxy, but that feature is coming soon.
24
+ 4. Any response headers that might come from the proxy will be saved in `response.headers`
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "scrapy-proxy-headers"
7
- version = "0.1.2"
7
+ version = "0.1.3"
8
8
  authors = [
9
9
  { name="ProxyMesh", email="support@proxymesh.com" },
10
10
  ]
@@ -23,3 +23,4 @@ classifiers = [
23
23
  [project.urls]
24
24
  Homepage = "https://github.com/proxymesh/scrapy-proxy-headers"
25
25
  Changelog = "https://github.com/proxymesh/scrapy-proxy-headers/commits/main/"
26
+ Issues = "https://github.com/proxymesh/scrapy-proxy-headers/issues"
@@ -1,6 +1,8 @@
1
- from scrapy.core.downloader.handlers.http11 import TunnelingAgent, TunnelingTCP4ClientEndpoint, ScrapyAgent, HTTP11DownloadHandler
1
+ import re
2
+ from scrapy.core.downloader.handlers.http11 import TunnelingAgent, TunnelingTCP4ClientEndpoint, ScrapyAgent
2
3
  from scrapy.core.downloader.webclient import _parse
3
4
  from scrapy.utils.python import to_bytes
5
+ from scrapy.http import Headers, Response
4
6
 
5
7
  def tunnel_request_data_with_headers(host: str, port: int, **proxy_headers) -> bytes:
6
8
  r"""
@@ -54,6 +56,19 @@ class TunnelingHeadersTCP4ClientEndpoint(TunnelingTCP4ClientEndpoint):
54
56
  protocol.dataReceived = self.processProxyResponse # type: ignore[method-assign]
55
57
  self._protocol = protocol
56
58
  return protocol
59
+
60
+ def processProxyResponse(self, data: bytes):
61
+ # data might have proxy headers, looks like
62
+ # b'HTTP/1.1 200 Connection established\r\nProxy-Header: VALUE\r\n\r\n'
63
+ response_headers = {}
64
+
65
+ for line in data.split(b'\r\n'):
66
+ if b':' in line:
67
+ key, val = line.split(b':', 1)
68
+ response_headers[key.strip()] = val.strip()
69
+ # save for endpoing & agent
70
+ self._proxy_response_headers = Headers(response_headers)
71
+ return super(TunnelingHeadersTCP4ClientEndpoint, self).processProxyResponse(data)
57
72
 
58
73
  class TunnelingHeadersAgent(TunnelingAgent):
59
74
  """An agent that uses a L{TunnelingTCP4ClientEndpoint} to make HTTPS
@@ -70,7 +85,8 @@ class TunnelingHeadersAgent(TunnelingAgent):
70
85
  self._proxy_headers = proxy_headers
71
86
 
72
87
  def _getEndpoint(self, uri):
73
- return TunnelingHeadersTCP4ClientEndpoint(
88
+ # save endpoint for agent to get proxy_response_headers
89
+ self._endpoint = TunnelingHeadersTCP4ClientEndpoint(
74
90
  reactor=self._reactor,
75
91
  host=uri.host,
76
92
  port=uri.port,
@@ -80,31 +96,32 @@ class TunnelingHeadersAgent(TunnelingAgent):
80
96
  bindAddress=self._endpointFactory._bindAddress,
81
97
  **self._proxy_headers
82
98
  )
99
+ return self._endpoint
83
100
 
84
101
  class ScrapyProxyHeadersAgent(ScrapyAgent):
85
102
  _TunnelingAgent = TunnelingHeadersAgent
103
+
104
+ def __init__(self, *args, **kwargs):
105
+ super().__init__(*args, **kwargs)
106
+ self._agent = None
86
107
 
87
108
  def _get_agent(self, request, timeout: float):
88
- agent = super()._get_agent(request, timeout)
109
+ self._agent = super()._get_agent(request, timeout)
89
110
 
90
111
  proxy = request.meta.get("proxy")
91
112
  proxy_headers = request.meta.get('proxy_headers')
92
113
  if proxy and proxy_headers:
93
114
  scheme = _parse(request.url)[0]
94
115
  if scheme == b"https":
95
- agent.set_proxy_headers(proxy_headers)
116
+ self._agent.set_proxy_headers(proxy_headers)
96
117
 
97
- return agent
118
+ return self._agent
98
119
 
99
- class HTTP11ProxyDownloadHandler(HTTP11DownloadHandler):
100
- def download_request(self, request, spider):
101
- """Return a deferred for the HTTP download"""
102
- agent = ScrapyProxyHeadersAgent(
103
- contextFactory=self._contextFactory,
104
- pool=self._pool,
105
- maxsize=getattr(spider, "download_maxsize", self._default_maxsize),
106
- warnsize=getattr(spider, "download_warnsize", self._default_warnsize),
107
- fail_on_dataloss=self._fail_on_dataloss,
108
- crawler=self._crawler,
109
- )
110
- return agent.download_request(request)
120
+ def _cb_bodydone(self, result, request, url: str):
121
+ r = super()._cb_bodydone(result, request, url)
122
+ if isinstance(r, Response):
123
+ if self._agent and hasattr(self._agent, '_endpoint'):
124
+ proxy_response_headers = getattr(self._agent._endpoint, '_proxy_response_headers')
125
+ if proxy_response_headers:
126
+ r.headers.update(proxy_response_headers)
127
+ return r
@@ -1,10 +1,11 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.1
2
2
  Name: scrapy-proxy-headers
3
- Version: 0.1.2
3
+ Version: 0.1.3
4
4
  Summary: Add custom proxy headers to HTTPS requests in Scrapy
5
5
  Author-email: ProxyMesh <support@proxymesh.com>
6
6
  Project-URL: Homepage, https://github.com/proxymesh/scrapy-proxy-headers
7
7
  Project-URL: Changelog, https://github.com/proxymesh/scrapy-proxy-headers/commits/main/
8
+ Project-URL: Issues, https://github.com/proxymesh/scrapy-proxy-headers/issues
8
9
  Classifier: Programming Language :: Python :: 3
9
10
  Classifier: Operating System :: OS Independent
10
11
  Classifier: License :: OSI Approved :: BSD License
@@ -23,7 +24,7 @@ Because Scrapy does not have a good way to pass custom headers to a proxy when y
23
24
 
24
25
  To use this extension, do the following:
25
26
 
26
- 1. `pip install scrapy_proxy_headers`
27
+ 1. `pip install scrapy-proxy-headers`
27
28
  2. In your Scrapy `settings.py`, add the following code:
28
29
 
29
30
  ```python
@@ -38,4 +39,4 @@ DOWNLOAD_HANDLERS = {
38
39
  request.meta["proxy_headers"] = {"X-ProxyMesh-Country": "US"}
39
40
  ```
40
41
 
41
- Currently this package does not yet support reading custom response headers from the proxy, but that feature is coming soon.
42
+ 4. Any response headers that might come from the proxy will be saved in `response.headers`