PyPAC - Python Proxy Auto-Config
PyPAC is a Python library that provides proxy auto-config (PAC) and proxy auto-discovery (WPAD) functionality. It enables applications to automatically determine and use the correct proxy server based on a PAC script, crucial for environments requiring complex proxy logic. The current version is 0.17.2, and it maintains a moderate release cadence, with several minor releases annually addressing bug fixes and minor enhancements.
Common errors
-
pypac.parser.proxy_resolver.ProxyResolutionError: No PAC file found.
cause PyPAC could not locate a PAC file through auto-discovery (WPAD) or no explicit PAC URL/path was provided.fixProvide the PAC file's URL or local path explicitly: `resolver = PACProxyResolver(url='http://your.proxy.com/proxy.pac')` or `session = PACSession(pac_url='file:///path/to/proxy.pac')`. -
AttributeError: 'PACSession' object has no attribute 'proxies'
cause `PACSession` from `pypac` is designed to handle proxy resolution internally and does not expose a settable `.proxies` attribute like a standard `requests.Session` object.fixDo not attempt to set or access `session.proxies` directly. `PACSession` automatically applies the resolved proxy. If you need to see what proxy was used, inspect `response.request.proxy_url` after a request has been made. -
ModuleNotFoundError: No module named 'requests'
cause You are trying to use `pypac.PACSession` but the `requests` library is not installed.fix`PACSession` integrates with `requests`. Install it with: `pip install requests` (or `pip install pypac requests`).
Warnings
- gotcha When using `PACSession` for `requests` integration, do not attempt to set `session.proxies` manually. `PACSession` transparently handles proxy resolution and setting, and directly modifying `session.proxies` will override or conflict with its functionality.
- gotcha PAC file auto-discovery (WPAD) can be unreliable or disabled in some network environments (e.g., corporate networks, firewalls). If `PACSession` or `PACProxyResolver` cannot find a PAC file, it will fall back to a direct connection, which might not be desired.
- gotcha Python 2.7 support is deprecated and requires the `dukpy` library for PAC script evaluation, which itself has specific installation and versioning requirements for older Python. For modern use, Python 3 is strongly recommended.
Install
-
pip install pypac -
pip install pypac requests
Imports
- PACProxyResolver
from pypac import PACProxyResolver
- PACSession
from pypac import PACSession
Quickstart
from pypac import PACSession
import requests
try:
# Auto-discovers PAC files or manually specify via PACSession(pac_url='...')
with PACSession() as session:
# Example usage with a public IP checker (ensure it's HTTP/HTTPS)
response = session.get('http://httpbin.org/ip')
response.raise_for_status()
print(f"Using PACSession, IP is: {response.json()['origin']}")
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")