scrapy-proxy-headers 0.1.0__py3-none-any.whl
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.
- scrapy_proxy_headers/__init__.py +13 -0
- scrapy_proxy_headers/agent.py +112 -0
- scrapy_proxy_headers/download_handler.py +15 -0
- scrapy_proxy_headers-0.1.0.dist-info/LICENSE +28 -0
- scrapy_proxy_headers-0.1.0.dist-info/METADATA +8 -0
- scrapy_proxy_headers-0.1.0.dist-info/RECORD +9 -0
- scrapy_proxy_headers-0.1.0.dist-info/WHEEL +5 -0
- scrapy_proxy_headers-0.1.0.dist-info/entry_points.txt +2 -0
- scrapy_proxy_headers-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""
|
|
2
|
+
To use this, in your settings, do the following:
|
|
3
|
+
|
|
4
|
+
DOWNLOAD_HANDLERS = {
|
|
5
|
+
"https": "scrapy_proxy_headers.HTTP11ProxyDownloadHandler"
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
Then when you make a request with a custom proxy header, instead of using request.headers, use request.meta["proxy_headers"] like this:
|
|
9
|
+
|
|
10
|
+
request.meta["proxy_headers"] = {"X-ProxyMesh-Country": "US"}
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from .download_handler import HTTP11ProxyDownloadHandler
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# TODO: handle response headers
|
|
2
|
+
|
|
3
|
+
from scrapy.core.downloader.handlers.http11 import TunnelingAgent, TunnelingTCP4ClientEndpoint, ScrapyAgent, HTTP11DownloadHandler
|
|
4
|
+
from scrapy.core.downloader.webclient import _parse
|
|
5
|
+
from scrapy.utils.python import to_bytes
|
|
6
|
+
|
|
7
|
+
def tunnel_request_data_with_headers(host: str, port: int, **proxy_headers) -> bytes:
|
|
8
|
+
r"""
|
|
9
|
+
Return binary content of a CONNECT request.
|
|
10
|
+
|
|
11
|
+
>>> from scrapy.utils.python import to_unicode as s
|
|
12
|
+
>>> s(tunnel_request_data_with_headers("example.com", 8080))
|
|
13
|
+
'CONNECT example.com:8080 HTTP/1.1\r\nHost: example.com:8080\r\n\r\n'
|
|
14
|
+
>>> s(tunnel_request_data_with_headers("example.com", 8080, **{"X-ProxyMesh-Country": "US"}))
|
|
15
|
+
'CONNECT example.com:8080 HTTP/1.1\r\nHost: example.com:8080\r\nX-ProxyMesh-Country: US\r\n\r\n'
|
|
16
|
+
>>> s(tunnel_request_data_with_headers(b"example.com", "8090"))
|
|
17
|
+
'CONNECT example.com:8090 HTTP/1.1\r\nHost: example.com:8090\r\n\r\n'
|
|
18
|
+
"""
|
|
19
|
+
host_value = to_bytes(host, encoding="ascii") + b":" + to_bytes(str(port))
|
|
20
|
+
tunnel_req = b"CONNECT " + host_value + b" HTTP/1.1\r\n"
|
|
21
|
+
tunnel_req += b"Host: " + host_value + b"\r\n"
|
|
22
|
+
|
|
23
|
+
for key, val in proxy_headers.items():
|
|
24
|
+
tunnel_req += to_bytes(key) + b": " + to_bytes(val) + b"\r\n"
|
|
25
|
+
|
|
26
|
+
tunnel_req += b"\r\n"
|
|
27
|
+
return tunnel_req
|
|
28
|
+
|
|
29
|
+
class TunnelingHeadersTCP4ClientEndpoint(TunnelingTCP4ClientEndpoint):
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
reactor,
|
|
33
|
+
host: str,
|
|
34
|
+
port: int,
|
|
35
|
+
proxyConf,
|
|
36
|
+
contextFactory,
|
|
37
|
+
timeout: float = 30,
|
|
38
|
+
bindAddress = None,
|
|
39
|
+
**proxy_headers
|
|
40
|
+
):
|
|
41
|
+
super().__init__(reactor, host, port, proxyConf, contextFactory, timeout, bindAddress)
|
|
42
|
+
|
|
43
|
+
self._proxy_headers = {}
|
|
44
|
+
if self._proxyAuthHeader:
|
|
45
|
+
self._proxy_headers['Proxy-Authorization'] = self._proxyAuthHeader
|
|
46
|
+
self._proxy_headers.update(proxy_headers)
|
|
47
|
+
|
|
48
|
+
def requestTunnel(self, protocol):
|
|
49
|
+
"""Asks the proxy to open a tunnel."""
|
|
50
|
+
assert protocol.transport
|
|
51
|
+
tunnelReq = tunnel_request_data_with_headers(
|
|
52
|
+
self._tunneledHost, self._tunneledPort, **self._proxy_headers
|
|
53
|
+
)
|
|
54
|
+
protocol.transport.write(tunnelReq)
|
|
55
|
+
self._protocolDataReceived = protocol.dataReceived
|
|
56
|
+
protocol.dataReceived = self.processProxyResponse # type: ignore[method-assign]
|
|
57
|
+
self._protocol = protocol
|
|
58
|
+
return protocol
|
|
59
|
+
|
|
60
|
+
class TunnelingHeadersAgent(TunnelingAgent):
|
|
61
|
+
"""An agent that uses a L{TunnelingTCP4ClientEndpoint} to make HTTPS
|
|
62
|
+
downloads. It may look strange that we have chosen to subclass Agent and not
|
|
63
|
+
ProxyAgent but consider that after the tunnel is opened the proxy is
|
|
64
|
+
transparent to the client; thus the agent should behave like there is no
|
|
65
|
+
proxy involved.
|
|
66
|
+
"""
|
|
67
|
+
def __init__(self, *args, **kwargs):
|
|
68
|
+
super().__init__(*args, **kwargs)
|
|
69
|
+
self._proxy_headers = {}
|
|
70
|
+
|
|
71
|
+
def set_proxy_headers(self, proxy_headers):
|
|
72
|
+
self._proxy_headers = proxy_headers
|
|
73
|
+
|
|
74
|
+
def _getEndpoint(self, uri):
|
|
75
|
+
return TunnelingHeadersTCP4ClientEndpoint(
|
|
76
|
+
reactor=self._reactor,
|
|
77
|
+
host=uri.host,
|
|
78
|
+
port=uri.port,
|
|
79
|
+
proxyConf=self._proxyConf,
|
|
80
|
+
contextFactory=self._contextFactory,
|
|
81
|
+
timeout=self._endpointFactory._connectTimeout,
|
|
82
|
+
bindAddress=self._endpointFactory._bindAddress,
|
|
83
|
+
**self._proxy_headers
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
class ScrapyProxyHeadersAgent(ScrapyAgent):
|
|
87
|
+
_TunnelingAgent = TunnelingHeadersAgent
|
|
88
|
+
|
|
89
|
+
def _get_agent(self, request, timeout: float):
|
|
90
|
+
agent = super()._get_agent(request, timeout)
|
|
91
|
+
|
|
92
|
+
proxy = request.meta.get("proxy")
|
|
93
|
+
proxy_headers = request.meta.get('proxy_headers')
|
|
94
|
+
if proxy and proxy_headers:
|
|
95
|
+
scheme = _parse(request.url)[0]
|
|
96
|
+
if scheme == b"https":
|
|
97
|
+
agent.set_proxy_headers(proxy_headers)
|
|
98
|
+
|
|
99
|
+
return agent
|
|
100
|
+
|
|
101
|
+
class HTTP11ProxyDownloadHandler(HTTP11DownloadHandler):
|
|
102
|
+
def download_request(self, request, spider):
|
|
103
|
+
"""Return a deferred for the HTTP download"""
|
|
104
|
+
agent = ScrapyProxyHeadersAgent(
|
|
105
|
+
contextFactory=self._contextFactory,
|
|
106
|
+
pool=self._pool,
|
|
107
|
+
maxsize=getattr(spider, "download_maxsize", self._default_maxsize),
|
|
108
|
+
warnsize=getattr(spider, "download_warnsize", self._default_warnsize),
|
|
109
|
+
fail_on_dataloss=self._fail_on_dataloss,
|
|
110
|
+
crawler=self._crawler,
|
|
111
|
+
)
|
|
112
|
+
return agent.download_request(request)
|
|
@@ -0,0 +1,15 @@
|
|
|
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)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, ProxyMesh
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
scrapy_proxy_headers/__init__.py,sha256=RO6DRVwJYVkNlaFT_FA8yD7PN4MKT8aPXaLz6yQDAE4,400
|
|
2
|
+
scrapy_proxy_headers/agent.py,sha256=PSAjJlZaCDaZGCp3ulkuA8bADIIzCgsPp_lG26Xxs3I,4505
|
|
3
|
+
scrapy_proxy_headers/download_handler.py,sha256=l_Kf8pR5Pv-vLHm6v-i7GcZhqfSb8YXtzOot_og5VX4,723
|
|
4
|
+
scrapy_proxy_headers-0.1.0.dist-info/LICENSE,sha256=m4ejt-XtSe45rEih_mlIn6Ta781QM_5evHyAJICwjaU,1496
|
|
5
|
+
scrapy_proxy_headers-0.1.0.dist-info/METADATA,sha256=cZ7gwy2KLq6rSdX-uLnbtPpdAzp4A5-6f3rSSkRzHhg,210
|
|
6
|
+
scrapy_proxy_headers-0.1.0.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
|
|
7
|
+
scrapy_proxy_headers-0.1.0.dist-info/entry_points.txt,sha256=u30iMIGFF-vpT0iaxOhyGWdReaRvcolUIaF4IkXXJ20,85
|
|
8
|
+
scrapy_proxy_headers-0.1.0.dist-info/top_level.txt,sha256=e-iRiYZDn-Lvf90KnVgK3IB7JhclZUoZmAucV04shqY,21
|
|
9
|
+
scrapy_proxy_headers-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
scrapy_proxy_headers
|