0

I want to access my company's private website through Python. I have a file mycode.py with the following script inside

import urllib.request

url = 'https://my-company-website.com'

with urllib.request.urlopen(url) as response:
   html = response.read()
   print(html)

I have included my corporate proxy in global system environment variables HTTP_PROXY and HTTPS_PROXY.

When I try and run this code, either through cmd as "python mycode.py" or by running a Jupyter notebook cell with this code inside I get the same error which is "Tunnel connection failed: 503 Service Unavailable". But when I run "python mycode.py" through cygwin it works just as expected.

Can this be explained by a simple reason or what could the reasons for this behaviour be?

Ant
  • 1
  • Did your Jupyter notebook use the same version of Python and urllib version as your cygwin? For Python, you can just check it by launching python shell, or by `import sys`, then `print (sys.version)`. For urllib, you can check it by `import urllib.request`, then `print (urllib.request.__version__)` – AlexanderK1987 Apr 26 '22 at 11:08
  • It seems that the environment variables should be in lower case, i.e., `http_proxy`. There is a similar question https://stackoverflow.com/questions/3168171/how-can-i-open-a-website-with-urllib-via-proxy-in-python . See if the answer help you. :D – AlexanderK1987 Apr 26 '22 at 11:10
  • It seems that they use different versions. In Jupyter I have python 3.9.7 and urllib 3.9 while in cygwin I have Python 3.8.2 and urllib I do not know since the command that version of urllib doesn't have an attribute "requests". But maybe this explains the behaviour then. Thank you for your answer. – Ant Apr 26 '22 at 11:28
  • I unfortunately did not get http_proxy to work with the urllib, no matter if it was given in upper or lower case. Maybe I have misunderstood something though :) – Ant Apr 26 '22 at 11:36
  • Yeah, a lot of problems have been reported with `urllib`, would you mind trying `requests` instead? https://docs.python-requests.org/en/latest/ – AlexanderK1987 Apr 26 '22 at 12:03

0 Answers0