How to use Proxy PAC file for python urllib or request?
Asked Answered
C

3

9

How do I include my automatic proxy config file in HTTP libraries like urllib or requests.

pacfile = 'http://myintranet.com/proxies/ourproxies.pac'
proxy = urllib3.ProxyManager(????????????????)
Courtly answered 21/7, 2015 at 1:16 Comment(0)
T
11

Current there is no support for a proxy PAC file directly in urllib3 or requests. While support could in principle be added for proxy PAC files, because they are Javascript files that require interpretation it is likely to be extremely difficult to provide broad-based support.

In principle you could use requests/urllib3 to request the Proxy PAC file, then pass it to something like Node.JS for interpreting, then parse the results back in Python to pass to urllib3/requests, but nothing like that exists out of the box.

Tendance answered 21/7, 2015 at 8:43 Comment(0)
A
13

I've created a pure-Python library called PyPAC which should do what you're looking for. It provides a subclass of requests.Session that includes honours PACs and includes PAC auto-discovery.

Axenic answered 22/5, 2017 at 5:44 Comment(3)
How is the best pratice to use pypac in requests?Checkbook
i'm tinkering with monkey patching urlllib. it's the only legit way to cover everything, since you may be using a library that you can't modify, which, in turn, uses requests or urllib or both.Peregrinate
@ErikAronesty pypac.pac_context_for_url() is a bit of a cheat way to get libraries working under a PAC network, as long as those libraries recognize proxy environment variables. It's not perfect, but it'll get people over the PAC hurdle in most cases.Axenic
T
11

Current there is no support for a proxy PAC file directly in urllib3 or requests. While support could in principle be added for proxy PAC files, because they are Javascript files that require interpretation it is likely to be extremely difficult to provide broad-based support.

In principle you could use requests/urllib3 to request the Proxy PAC file, then pass it to something like Node.JS for interpreting, then parse the results back in Python to pass to urllib3/requests, but nothing like that exists out of the box.

Tendance answered 21/7, 2015 at 8:43 Comment(0)
R
5

Use PYPAC.

from pypac import PACSession, get_pac

pac = get_pac(url='http://your/pac/url/file.pac')
session = PACSession(pac, proxy_auth=HTTPProxyAuth('your_user', 'password'))
print(session.get('http://www.google.com'))

you will get a 200

Rummel answered 10/3, 2021 at 3:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.