Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡️ Speed up function select_proxy by 22% #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions src/requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
UnrewindableBodyError,
)
from .structures import CaseInsensitiveDict
from urllib.parse import urlparse

NETRC_FILES = (".netrc", "_netrc")

Expand Down Expand Up @@ -844,22 +845,19 @@ def select_proxy(url, proxies):
:param url: The url being for the request
:param proxies: A dictionary of schemes or schemes and hosts to proxy URLs
"""
proxies = proxies or {}
if not proxies:
return None

urlparts = urlparse(url)
if urlparts.hostname is None:
hostname = urlparts.hostname

if not hostname:
return proxies.get(urlparts.scheme, proxies.get("all"))

proxy_keys = [
urlparts.scheme + "://" + urlparts.hostname,
urlparts.scheme,
"all://" + urlparts.hostname,
"all",
]
proxy = None
for proxy_key in proxy_keys:
if proxy_key in proxies:
proxy = proxies[proxy_key]
break
proxy = (proxies.get(f"{urlparts.scheme}://{hostname}") or
proxies.get(urlparts.scheme) or
proxies.get(f"all://{hostname}") or
proxies.get("all"))

return proxy

Expand Down