-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
697610d
commit a95d97a
Showing
8 changed files
with
85 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from django.conf import settings | ||
from django.utils.module_loading import import_string | ||
from urllib.parse import urlparse | ||
|
||
__all__ = ( | ||
'DefaultProxyRouter', | ||
'resolve_proxies', | ||
) | ||
|
||
|
||
class DefaultProxyRouter: | ||
""" | ||
Base class for a proxy router. | ||
""" | ||
@staticmethod | ||
def _get_protocol_from_url(url): | ||
""" | ||
Determine the applicable protocol (e.g. HTTP or HTTPS) from the given URL. | ||
""" | ||
return urlparse(url).scheme | ||
|
||
def route(self, url=None, protocol=None, context=None): | ||
if url and protocol is None: | ||
protocol = self._get_protocol_from_url(url) | ||
if protocol and protocol in settings.HTTP_PROXIES: | ||
return { | ||
protocol: settings.HTTP_PROXIES[protocol] | ||
} | ||
return settings.HTTP_PROXIES | ||
|
||
|
||
def resolve_proxies(url=None, protocol=None, context=None): | ||
""" | ||
Return a dictionary of candidate proxies (compatible with the requests module), or None. | ||
Args: | ||
url: The specific request URL for which the proxy will be used (optional) | ||
protocol: The protocol in use (e.g. http or https) (optional) | ||
context: Arbitrary additional context to aid in proxy selection (optional) | ||
""" | ||
context = context or {} | ||
|
||
for item in settings.PROXY_ROUTERS: | ||
router = import_string(item) if type(item) is str else item | ||
if proxies := router.route(url=url, protocol=protocol, context=context): | ||
return proxies |