Core
proxylib.proxy
Core proxy/URI types: parsing proxy strings and mapping request URLs to proxies.
Proxy models a single scheme://[user[:pass]@]host[:port] proxy entry
(as found in PROXY/env-var/PAC proxy strings). ProxyMap is the
protocol every proxy-selection strategy in this library implements
(EnvProxyConfig, pac.PAC, SimpleProxyMap): given a request URL it
returns the sequence of Proxy (or None for DIRECT) to try, in order.
__all__ = ['Proxy', 'ProxyMap', 'UriSplit', 'SimpleProxyMap', 'ChainProxyMap', 'ConfigurableProxyMap']
module-attribute
ChainProxyMap(*maps)
Bases: ProxyMap
Tries each ProxyMap in order; the first one with an opinion wins.
Works because of the codified result contract (see ProxyMap's
docstring): a constituent map's KeyError means "no opinion, try the
next one", while a [None]/[Proxy] result is definitive and
stops the chain immediately -- e.g. ChainProxyMap(EnvProxyConfig(...),
pac_map) falls through to the PAC map only when the env config has no
proxy configured for that scheme, not merely when it resolves to DIRECT.
If every constituent map raises KeyError, the chain does too --
"no opinion" composes, so a ChainProxyMap can itself be one link in
a larger chain.
Source code in src/proxylib/proxy.py
206 207 | |
__slots__ = ('maps',)
class-attribute
instance-attribute
maps = maps
instance-attribute
__getitem__(uri)
Source code in src/proxylib/proxy.py
209 210 211 212 213 214 215 | |
ConfigurableProxyMap(proxymap, *, cache_ttl=None, probe=False, probe_timeout=5.0, round_robin=False, browser_compatibility=False, bypass_local=False, no_proxy=None)
Bases: ProxyMap
Decorates any ProxyMap with caching, active probing, round-robin
selection, browser-style privacy stripping, and an implicit local bypass.
All features are opt-in and independent; the decorated map's own result
contract (KeyError/[None]/[Proxy]) is preserved throughout.
Source code in src/proxylib/proxy.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
browser_compatibility = browser_compatibility
instance-attribute
cache_ttl = cache_ttl
instance-attribute
probe = probe
instance-attribute
probe_timeout = probe_timeout
instance-attribute
proxymap = proxymap
instance-attribute
round_robin = round_robin
instance-attribute
__getitem__(url)
Source code in src/proxylib/proxy.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
clear_cache()
Source code in src/proxylib/proxy.py
269 270 | |
Proxy
Bases: _URI
A single proxy authority, e.g. http://user:pass@proxy.example.com:8080.
Proxy("direct", ...) returns None (the sentinel used everywhere in
this library to mean "connect without a proxy") rather than an instance.
url
property
__new__(scheme, username, password, host, port)
Source code in src/proxylib/proxy.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
ProxyMap
Bases: Protocol
Protocol for "given a request URL, which proxies should I try?".
Calling ProxyMap(src) is a small factory: a plain proxy-authority
string builds a SimpleProxyMap, but a string that looks like a URL to
a PAC file/script is loaded as one (see :mod:proxylib.pac).
Result contract (every implementation must follow this so chaining multiple maps together is meaningful):
__getitem__raisesKeyErrorto mean no decision -- this map has no opinion on the request, distinct from an explicit answer. A fallback/chain should try the next map.__getitem__yieldsNoneas an entry to mean DIRECT (no proxy) -- an explicit, definitive answer, not "no opinion".__getitem__yields aProxyentry to mean use this proxy.
Per-implementation notes: SimpleProxyMap and pac.PAC are always
definitive (constructed with, or computed, an explicit answer) and never
raise KeyError. EnvProxyConfig raises KeyError for a scheme
with no configured env proxy (env vars can't express "explicitly
DIRECT", only "set" or "absent").
__contains__(key)
Source code in src/proxylib/proxy.py
117 118 119 120 121 122 | |
__enter__()
Source code in src/proxylib/proxy.py
124 125 126 127 128 129 130 131 | |
__exit__(exc_type, exc_val, exc_tb)
Source code in src/proxylib/proxy.py
133 134 135 136 137 | |
__getitem__(uri)
Source code in src/proxylib/proxy.py
108 109 | |
__new__(*args, **kwargs)
Source code in src/proxylib/proxy.py
95 96 97 98 99 100 101 102 103 104 105 106 | |
get(uri, default=None)
Source code in src/proxylib/proxy.py
111 112 113 114 115 | |
SimpleProxyMap(proxy=None)
Bases: ProxyMap
A ProxyMap that always returns the same fixed proxy (or list of proxies).
Source code in src/proxylib/proxy.py
171 172 173 174 175 176 177 178 179 180 181 182 183 | |
proxies = (proxy,)
instance-attribute
__getitem__(uri)
Source code in src/proxylib/proxy.py
185 186 | |
UriSplit
Bases: Enum
Regexes for splitting either a plain URI or a PAC PROXY ...; ... string.
Default = re.compile(f'{DELIM}(?:(?:({SCHEME}):)?(?://{AUTHORITY})?\s*)')
class-attribute
instance-attribute
PAC = re.compile(f'{DELIM}({SCHEME})(?:\s+(?:{AUTHORITY})?\s*)?')
class-attribute
instance-attribute
findall(uri)
Source code in src/proxylib/_uri.py
37 38 | |
match(uri)
Source code in src/proxylib/_uri.py
34 35 | |
proxylib.env
Proxy selection from the conventional HTTP_PROXY/HTTPS_PROXY/NO_PROXY env vars.
__all__ = ('EnvProxyConfig', 'set_default_no_proxy', 'get_default_no_proxy')
module-attribute
EnvProxyConfig(http_proxy, https_proxy, no_proxy)
Bases: ProxyMap
A ProxyMap built from HTTP_PROXY/HTTPS_PROXY/NO_PROXY-style settings.
Source code in src/proxylib/env.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
__slots__ = ('http_proxy', 'https_proxy', 'no_proxy')
class-attribute
instance-attribute
http_proxy = SimpleProxyMap(http_proxy) if http_proxy else None
instance-attribute
https_proxy = SimpleProxyMap(https_proxy) if https_proxy else None
instance-attribute
no_proxy = [(_parse_no_proxy_entry(_no)) for _no in merged]
instance-attribute
__getitem__(url)
Source code in src/proxylib/env.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
from_env()
staticmethod
Build an EnvProxyConfig from the process environment (upper or lowercase names).
Source code in src/proxylib/env.py
145 146 147 148 149 150 151 152 153 154 | |
get_default_no_proxy()
Return a copy of the current process-wide default NO_PROXY rules.
Source code in src/proxylib/env.py
33 34 35 | |
set_default_no_proxy(rules)
Set the process-wide default NO_PROXY rules (same entry syntax as the
env var: hostnames, .suffix, <local>, CIDR). Merged with -- not
replaced by -- whatever rules each EnvProxyConfig/ConfigurableProxyMap
is given explicitly. Pass None (or an empty iterable) to clear it.
Source code in src/proxylib/env.py
24 25 26 27 28 29 30 | |
proxylib.netutils
Small stdlib-first networking helpers used across proxylib.
get_local_interfaces prefers the optional ifaddr package (accurate
adapter/prefix info on every OS) and falls back to a best-effort,
stdlib-only implementation when it isn't installed.
clear_circuit_breaker()
Source code in src/proxylib/netutils.py
156 157 | |
clear_interfaces_cache()
Clear the get_local_interfaces cache. Call this in tests that monkeypatch enumeration.
Source code in src/proxylib/netutils.py
98 99 100 101 | |
first_working_proxy(proxies, timeout=5.0, circuit_breaker_ttl=30.0, clock=_time.monotonic)
Return the first entry that accepts a TCP connection, in order.
Failover helper for ProxyMap results (PROXY a; PROXY b; DIRECT
means "try these in order")::
proxy = first_working_proxy(proxymap[url])
A None entry (DIRECT) is returned immediately -- there's no proxy to
probe. This only checks TCP reachability of the proxy port, not that the
proxy will actually serve the request. Raises LookupError when no
entry is reachable (None can't signal failure here: it means DIRECT).
A proxy that fails its probe is blacklisted (skipped without probing)
for circuit_breaker_ttl seconds (default 30); pass 0/None to
disable.
Source code in src/proxylib/netutils.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
get_default_port(scheme)
Return the conventional port for a scheme, or None if unknown.
Source code in src/proxylib/netutils.py
136 137 138 139 140 141 142 143 144 | |
get_ip(address)
Resolve a hostname or literal address to an ip_address, or None.
Source code in src/proxylib/netutils.py
104 105 106 107 108 109 110 111 112 | |
get_local_interfaces(cache_ttl=10.0)
Return this host's local network interfaces, cached for cache_ttl seconds.
Pass cache_ttl=0/None to force a fresh enumeration.
Source code in src/proxylib/netutils.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
is_loopback_or_link_local(ip)
True for loopback (127/8, ::1) or link-local (169.254/16,
fe80::/10) addresses -- these can never usefully go through a proxy
(link-local is inherently confined to the local link). Shared by
env.py's <local> NO_PROXY handling and
ConfigurableProxyMap's bypass_local= option so the definition
only lives in one place.
Source code in src/proxylib/netutils.py
115 116 117 118 119 120 121 122 123 | |