carsonyl / pypac

Find and use proxy auto-config (PAC) files with Python and Requests.
https://pypac.readthedocs.io
Apache License 2.0
71 stars 19 forks source link

can't get the pac #50

Closed ZaytsevArtem closed 4 years ago

ZaytsevArtem commented 4 years ago

Hi, could you please help me to understand what I am doing wrong in my code? I can't understand why the pac is not get. I'm new in Python and have some trouble in understanding of the exceptions mechanics. Trying to guess how to check an exception, I used these variants:

try:
  _pac = get_pac(url = 'https://antizapret.prostovpn.org/proxy.pac')
except MalformedPacError as e:
  print(e.msg)
except Exception as e:
  print(e.__class__)
except:
  print('Unknown exception')
finally:
  if not _pac:
    print('pac is null')
  else:
    print('ok!')

I have none of exceptions to occur, but i get the pac is null message as a result. Is the problem in the specified pac-file code (it's pretty complicated)? But then PyPAC should tell me about it. And I wonder if I ever do have to check exceptions at all, aren't they to be shown automatically by the py compiler with the default settings? I use to get a lot of exceptions in my other py exercises. I'm confused and feel stupid.

Sorry if its a lame question and for my bad english also.

PS: Using python 3.7.3 in Windows if it matters.

carsonyl commented 4 years ago

In your code example, an exception is not triggered, so your exception-catching code does not run. get_pac() returns None if it does not think there is a PAC. As explained in the doc, PyPAC will not try to parse a PAC if the URL returns an unrecognized Content-Type. https://antizapret.prostovpn.org/proxy.pac has a Content-Type of application/javascript. So you can do this:

>>> from pypac import get_pac
>>> get_pac(url='https://antizapret.prostovpn.org/proxy.pac', allowed_content_types={'application/javascript'})
<pypac.parser.PACFile object at 0x0000017574339E48>