scrapy-proxy-headers 0.1.3__tar.gz → 0.1.4__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,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: scrapy-proxy-headers
3
- Version: 0.1.3
3
+ Version: 0.1.4
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
@@ -16,16 +16,20 @@ Requires-Python: >=3.8
16
16
  Description-Content-Type: text/markdown
17
17
  License-File: LICENSE
18
18
 
19
- The `scrapy-proxy-headers` package is designed for adding proxy headers to HTTPS requests.
19
+ # Scrapy Proxy Headers
20
+
21
+ The `scrapy-proxy-headers` package is designed for adding proxy headers to HTTPS requests in [Scrapy](https://scrapy.org/).
20
22
 
21
23
  In normal usage, custom headers put in `request.headers` cannot be read by a proxy when you make a HTTPS request, because the headers are encrypted and passed through the proxy tunnel, along with the rest of the request body. You can read more about this at [Proxy Server Requests over HTTPS](https://docs.proxymesh.com/article/145-proxy-server-requests-over-https).
22
24
 
23
- Because Scrapy does not have a good way to pass custom headers to a proxy when you make HTTPS requests, we at [ProxyMesh](https://proxymesh.com) made this extension to support our customers that use Scrapy and want to use custom headers to control our proxy behavior. But this extension can work for any custom headers through a proxy.
25
+ Because Scrapy does not have a good way to pass custom headers to a proxy when you make HTTPS requests, we at [ProxyMesh](https://proxymesh.com) made this extension to support our customers that use Scrapy and want to use custom headers to control our proxy behavior. But this extension can work for handling custom headers through any proxy.
26
+
27
+ ## Installation
24
28
 
25
29
  To use this extension, do the following:
26
30
 
27
31
  1. `pip install scrapy-proxy-headers`
28
- 2. In your Scrapy `settings.py`, add the following code:
32
+ 2. In your Scrapy `settings.py`, add the following:
29
33
 
30
34
  ```python
31
35
  DOWNLOAD_HANDLERS = {
@@ -33,10 +37,12 @@ DOWNLOAD_HANDLERS = {
33
37
  }
34
38
  ```
35
39
 
36
- 3. When you want make a request with a custom proxy header, instead of using `request.headers`, use `request.meta["proxy_headers"]` like this:
40
+ ## Usage
41
+
42
+ When you want make a request with a custom proxy header, instead of using `request.headers`, use `request.meta["proxy_headers"]` like this:
37
43
 
38
44
  ```python
39
45
  request.meta["proxy_headers"] = {"X-ProxyMesh-Country": "US"}
40
46
  ```
41
47
 
42
- 4. Any response headers that might come from the proxy will be saved in `response.headers`
48
+ Any response headers that might come from the proxy will be saved in `response.headers`, as in `response.headers["X-ProxyMesh-IP"]`.
@@ -1,13 +1,17 @@
1
- The `scrapy-proxy-headers` package is designed for adding proxy headers to HTTPS requests.
1
+ # Scrapy Proxy Headers
2
+
3
+ The `scrapy-proxy-headers` package is designed for adding proxy headers to HTTPS requests in [Scrapy](https://scrapy.org/).
2
4
 
3
5
  In normal usage, custom headers put in `request.headers` cannot be read by a proxy when you make a HTTPS request, because the headers are encrypted and passed through the proxy tunnel, along with the rest of the request body. You can read more about this at [Proxy Server Requests over HTTPS](https://docs.proxymesh.com/article/145-proxy-server-requests-over-https).
4
6
 
5
- Because Scrapy does not have a good way to pass custom headers to a proxy when you make HTTPS requests, we at [ProxyMesh](https://proxymesh.com) made this extension to support our customers that use Scrapy and want to use custom headers to control our proxy behavior. But this extension can work for any custom headers through a proxy.
7
+ Because Scrapy does not have a good way to pass custom headers to a proxy when you make HTTPS requests, we at [ProxyMesh](https://proxymesh.com) made this extension to support our customers that use Scrapy and want to use custom headers to control our proxy behavior. But this extension can work for handling custom headers through any proxy.
8
+
9
+ ## Installation
6
10
 
7
11
  To use this extension, do the following:
8
12
 
9
13
  1. `pip install scrapy-proxy-headers`
10
- 2. In your Scrapy `settings.py`, add the following code:
14
+ 2. In your Scrapy `settings.py`, add the following:
11
15
 
12
16
  ```python
13
17
  DOWNLOAD_HANDLERS = {
@@ -15,10 +19,12 @@ DOWNLOAD_HANDLERS = {
15
19
  }
16
20
  ```
17
21
 
18
- 3. When you want make a request with a custom proxy header, instead of using `request.headers`, use `request.meta["proxy_headers"]` like this:
22
+ ## Usage
23
+
24
+ When you want make a request with a custom proxy header, instead of using `request.headers`, use `request.meta["proxy_headers"]` like this:
19
25
 
20
26
  ```python
21
27
  request.meta["proxy_headers"] = {"X-ProxyMesh-Country": "US"}
22
28
  ```
23
29
 
24
- 4. Any response headers that might come from the proxy will be saved in `response.headers`
30
+ Any response headers that might come from the proxy will be saved in `response.headers`, as in `response.headers["X-ProxyMesh-IP"]`.
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "scrapy-proxy-headers"
7
- version = "0.1.3"
7
+ version = "0.1.4"
8
8
  authors = [
9
9
  { name="ProxyMesh", email="support@proxymesh.com" },
10
10
  ]
@@ -121,7 +121,9 @@ class ScrapyProxyHeadersAgent(ScrapyAgent):
121
121
  r = super()._cb_bodydone(result, request, url)
122
122
  if isinstance(r, Response):
123
123
  if self._agent and hasattr(self._agent, '_endpoint'):
124
- proxy_response_headers = getattr(self._agent._endpoint, '_proxy_response_headers')
124
+ proxy_response_headers = getattr(self._agent._endpoint, '_proxy_response_headers', None)
125
125
  if proxy_response_headers:
126
126
  r.headers.update(proxy_response_headers)
127
+ # save this for download handler
128
+ r._proxy_response_headers = proxy_response_headers
127
129
  return r
@@ -0,0 +1,37 @@
1
+ from scrapy.core.downloader.handlers.http11 import HTTP11DownloadHandler
2
+ from scrapy_proxy_headers.agent import ScrapyProxyHeadersAgent
3
+
4
+ class HTTP11ProxyDownloadHandler(HTTP11DownloadHandler):
5
+ def __init__(self, *args, **kwargs):
6
+ super().__init__(*args, **kwargs)
7
+ self._proxy_headers_by_proxy = {}
8
+
9
+ def download_request(self, request, spider):
10
+ """Return a deferred for the HTTP download"""
11
+ agent = ScrapyProxyHeadersAgent(
12
+ contextFactory=self._contextFactory,
13
+ pool=self._pool,
14
+ maxsize=getattr(spider, "download_maxsize", self._default_maxsize),
15
+ warnsize=getattr(spider, "download_warnsize", self._default_warnsize),
16
+ fail_on_dataloss=self._fail_on_dataloss,
17
+ crawler=self._crawler,
18
+ )
19
+ response = agent.download_request(request)
20
+ proxy = request.meta.get("proxy")
21
+
22
+ if proxy:
23
+ # we need to do all this because the proxy tunnels can get re-used
24
+ # when that happens, the proxy headers are not available in subsequent responses
25
+ # so we need to save the proxy headers by the proxy, from the first tunnel response
26
+ # so we can add them to subsequent responses
27
+ def callback(response):
28
+ if hasattr(response, '_proxy_response_headers'):
29
+ self._proxy_headers_by_proxy[proxy] = response._proxy_response_headers
30
+
31
+ if proxy in self._proxy_headers_by_proxy:
32
+ response.headers.update(self._proxy_headers_by_proxy[proxy])
33
+
34
+ return response
35
+
36
+ response.addCallback(callback)
37
+ return response
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: scrapy-proxy-headers
3
- Version: 0.1.3
3
+ Version: 0.1.4
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
@@ -16,16 +16,20 @@ Requires-Python: >=3.8
16
16
  Description-Content-Type: text/markdown
17
17
  License-File: LICENSE
18
18
 
19
- The `scrapy-proxy-headers` package is designed for adding proxy headers to HTTPS requests.
19
+ # Scrapy Proxy Headers
20
+
21
+ The `scrapy-proxy-headers` package is designed for adding proxy headers to HTTPS requests in [Scrapy](https://scrapy.org/).
20
22
 
21
23
  In normal usage, custom headers put in `request.headers` cannot be read by a proxy when you make a HTTPS request, because the headers are encrypted and passed through the proxy tunnel, along with the rest of the request body. You can read more about this at [Proxy Server Requests over HTTPS](https://docs.proxymesh.com/article/145-proxy-server-requests-over-https).
22
24
 
23
- Because Scrapy does not have a good way to pass custom headers to a proxy when you make HTTPS requests, we at [ProxyMesh](https://proxymesh.com) made this extension to support our customers that use Scrapy and want to use custom headers to control our proxy behavior. But this extension can work for any custom headers through a proxy.
25
+ Because Scrapy does not have a good way to pass custom headers to a proxy when you make HTTPS requests, we at [ProxyMesh](https://proxymesh.com) made this extension to support our customers that use Scrapy and want to use custom headers to control our proxy behavior. But this extension can work for handling custom headers through any proxy.
26
+
27
+ ## Installation
24
28
 
25
29
  To use this extension, do the following:
26
30
 
27
31
  1. `pip install scrapy-proxy-headers`
28
- 2. In your Scrapy `settings.py`, add the following code:
32
+ 2. In your Scrapy `settings.py`, add the following:
29
33
 
30
34
  ```python
31
35
  DOWNLOAD_HANDLERS = {
@@ -33,10 +37,12 @@ DOWNLOAD_HANDLERS = {
33
37
  }
34
38
  ```
35
39
 
36
- 3. When you want make a request with a custom proxy header, instead of using `request.headers`, use `request.meta["proxy_headers"]` like this:
40
+ ## Usage
41
+
42
+ When you want make a request with a custom proxy header, instead of using `request.headers`, use `request.meta["proxy_headers"]` like this:
37
43
 
38
44
  ```python
39
45
  request.meta["proxy_headers"] = {"X-ProxyMesh-Country": "US"}
40
46
  ```
41
47
 
42
- 4. Any response headers that might come from the proxy will be saved in `response.headers`
48
+ Any response headers that might come from the proxy will be saved in `response.headers`, as in `response.headers["X-ProxyMesh-IP"]`.
@@ -1,15 +0,0 @@
1
- from scrapy.core.downloader.handlers.http11 import HTTP11DownloadHandler
2
- from scrapy_proxy_headers.agent import ScrapyProxyHeadersAgent
3
-
4
- class HTTP11ProxyDownloadHandler(HTTP11DownloadHandler):
5
- def download_request(self, request, spider):
6
- """Return a deferred for the HTTP download"""
7
- agent = ScrapyProxyHeadersAgent(
8
- contextFactory=self._contextFactory,
9
- pool=self._pool,
10
- maxsize=getattr(spider, "download_maxsize", self._default_maxsize),
11
- warnsize=getattr(spider, "download_warnsize", self._default_warnsize),
12
- fail_on_dataloss=self._fail_on_dataloss,
13
- crawler=self._crawler,
14
- )
15
- return agent.download_request(request)