Skip to main content

Python package which builds on pypac to add pac file support for httplib2 Http

Project description

pypac4http2

Python package which builds on pypac to add pac file support for httplib2 Http

This package adds proxy auto configure pac support to the httplib2 library Http classby sub-classing the class and adding a new constructor parameter for a resolver class.

Installation

pip install pypac4http2

Usage

HttpPac Class

The HttpPac class extends httplib2.Http and automatically resolves the proxy for each request using a PAC file.

from pypac4http2 import HttpPac

# Automatically discover PAC using OS settings or WPAD
http = HttpPac()
response, content = http.request("http://example.org")

# Specify a PAC URL
http = HttpPac(pac_url="http://internal.corp/proxy.pac")
response, content = http.request("http://example.org")

# Provide PAC as a JavaScript string
pac_js = "function FindProxyForURL(url, host) { return 'PROXY proxy:8080'; }"
http = HttpPac(pac_js=pac_js)
response, content = http.request("http://example.org")

# The library also supports the PAC_URL environment variable:
# export PAC_URL=http://internal.corp/proxy.pac

Usage with Google API Client

HttpPac is designed to be a drop-in replacement for httplib2.Http, making it ideal for use with the Google API Python Client.

In a typical enterprise environment, you may also need your authentication flow (which often uses google-auth and requests) to respect the same PAC settings. You can use pypac.PACSession alongside HttpPac.

import os
from googleapiclient.discovery import build
import google.auth
from google.auth.transport.requests import Request
from pypac import PACSession, get_pac
from pypac4http2 import HttpPac

# 1. Use PACSession for authentication flows that use 'requests'
# pypac.PACSession doesn't natively check PAC_URL, so we get the PAC file manually
pac_url = os.environ.get("PAC_URL")
pac = get_pac(url=pac_url) if pac_url else get_pac()
auth_session = PACSession(pac=pac)
auth_request = Request(session=auth_session)

# 2. Get default credentials using the proxy-aware request
credentials, project = google.auth.default(request=auth_request)

# 3. Refresh credentials using the proxy-aware session
credentials.refresh(auth_request)

# 4. Use HttpPac for the Google API service calls (which use 'httplib2')
# Both will respect PAC settings (e.g. via PAC_URL environment variable)
http = HttpPac()
service = build('drive', 'v3', http=http, credentials=credentials)

# All API calls will now automatically resolve proxies via PAC
files = service.files().list().execute()

CLI Tool

The package includes a CLI tool pypac4http2 to resolve proxies for a given URL.

# Resolve proxy using OS auto-discovery (or PAC_URL env var if set)
pypac4http2 https://google.com

# Resolve proxy using a specific PAC URL
pypac4http2 --pac-url http://example.com/proxy.pac https://google.com

# Resolve proxy using a PAC JavaScript string
pypac4http2 --pac-js "function FindProxyForURL(url, host) { return 'DIRECT'; }" https://google.com

Output Example:

Proxy choice: PROXY proxy.example.com:8080
{
  "target_url": "https://google.com",
  "pac_result": "PROXY proxy.example.com:8080",
  "proxy_info": {
    "proxy_type": "HTTP",
    "proxy_host": "proxy.example.com",
    "proxy_port": 8080
  }
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypac4http2-0.0.7.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pypac4http2-0.0.7-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file pypac4http2-0.0.7.tar.gz.

File metadata

  • Download URL: pypac4http2-0.0.7.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pypac4http2-0.0.7.tar.gz
Algorithm Hash digest
SHA256 90cd9cdba9752041d16550ef8d3c3ef527bf6ca65fc239cb7c1ce16bf218053a
MD5 d5c9104ec039e32b32da97c4b3b0bd0b
BLAKE2b-256 0d407b306b2ab86d38a41717d6ad099dd278eb68c580a5fa6a19e8eaa3e25baa

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypac4http2-0.0.7.tar.gz:

Publisher: publish.yml on MikeMoore63/pypac4http2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pypac4http2-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: pypac4http2-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pypac4http2-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 9589bf22126ef9e59ec0d3d1540a99f47fc0917b6f93b811b62405bb08a3f302
MD5 4b866c6020653c66650004809207df9b
BLAKE2b-256 ecd12b4981c6e483bb9187a14b77cbffe4ac287d1f8a5fb19d346bb7d587baaa

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypac4http2-0.0.7-py3-none-any.whl:

Publisher: publish.yml on MikeMoore63/pypac4http2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page