Is there a native way in Python with the requests library to only use a proxy for a specific domain?
Like how you can mount HTTP Adapters, but with proxies, like the following example:
from requests import Session
from requests.adapters import HTTPAdapter
s = Session()
s.mount("http://www.example.org", HTTPAdapter(max_retries=retries))
CodePudding user response:
You could create a dictionary keyed on domains with proxies as associated values.
Let's assume you're interested in HTTP GET. Then you just write a wrapper around requests.get()
For example:
from requests import get as GET, packages as PACKAGES
from urllib.parse import urlparse as PARSE
PACKAGES.urllib3.util.ssl_.DEFAULT_CIPHERS = 'ALL:@SECLEVEL=1'
DOMAINS = {
'www.bbc.co.uk': 'http://115.144.101.200:10000'
}
def http_get(url):
proxy = DOMAINS.get(PARSE(url).netloc)
proxies = {url: proxy} if proxy is not None else None
print(f'{proxies=}')
return GET(url, proxies=proxies)
print(http_get('https://news.bbc.co.uk'))
print(http_get('https://www.bbc.co.uk'))
Output:
proxies=None
<Response [200]>
proxies={'https://www.bbc.co.uk': 'http://115.144.101.200:10000'}
<Response [200]>